I want to include a page that extracts some information from database into a home page and show results into home page, page by page, for example, the page who extract info print 20 result and it's too long to affiche all those results into the home page, so I want to make a section or anything into the home page and print only 5 result by 5 result and add a next and previous button to print all the others result without reloading the official home page,
there is my simple home page:
<html>
<!-- other objects in page-->
<section id="cnt">
<?php include('extract_infos.php'); ?>
</section>
</html>
and the page who extract infos to affiche him into the home page "extract_infos.php" is:
<?php
include 'db.php';
try{
$sql1 = "SELECT `name`, `ps`, `image` FROM `profile`";
$result1 = mysqli_query($con, $sql1) or die("Error:
".mysqli_error($con));
while($row1=mysqli_fetch_array($result1, MYSQLI_NUM)){ ?>
<p class="text-muted"><?php echo $row1[0]; ?></p>
<?php
}
mysqli_free_result($result1);
} catch (Exception $e) {
echo `Exception reçue : `.$e;
}
?>
the second page print for example 20 result and she print all of them into the home page,
what I want is: limit print by page and print only 5 results and when I click into next the home page doesn't reload but I see the next 5 result...
please I need help
You need some sort of pagination which can be done in many ways. For example you can do something like this,
Each time send the page number to your backend
-- $pageNumber
And then change your query to this:
$startingFrom = (int)$pageNumber*5
$sql = "SELECT `name`, `ps`, `image` FROM `profile` LIMIT $startingFrom,5"; # Retrieve 5 rows each time, starting from $startingFrom
For more information you can have a look at this page and follow these tutorials: 1, 2 or many similar ones you can find on the Web!
Related
I have search results and am displaying them in div on left of page. When user clicks on the a particular result (which is a link), it needs to open on the right of the page, without refreshing the page or moving to a new page. How does one do this?
//get rows
$query = $db->query("SELECT * FROM posts ORDER BY id DESC LIMIT $limit");
if($query->num_rows > 0){ ?>
<div class="post_list">
<?php
while($row = $query->fetch_assoc()){
$postID = $row['id'];
?>
<div class="list_item"><h2><?php echo $row["title"] ?></h2></div>
<?php } ?>
</div>
<?php echo $pagination->createLinks(); ?>
<?php } ?>
</div>
</div>
</body>
</html>
You are going to have an onclick listeners on each <a> which will take your file/file.php?id= as input to an ajax call to fetch the contents at file/file.php?id=.
I am not sure how familiar you are with Javascript, if you know it very well then you'll undersntand what I said above, if you don't then please find documentation about AJAX some ajax documentation
You might also want to find documentation on stuff like innerHTML dom manipulation
I have a site in HTML, where I implemented login and register system using PHP.
When there is no user logged in, the navigation item "Contul meu" from every page need to send me to "gotosigning.html" page.
However, when the user is logged in I want that the menu item "Contul meu" from every page to send me to "account.html" page.
Some menu items for example, in the index.html page:
<li class="active">Acasă</li>
<li>login</li>
<li>sign in</li>
<li>Contul meu</li>
the gotosigning.html page redirects to a page where you can select if you want to sign in or login, and based on the selection here you go to login.html or signin.html.
This is the basic example,when the user is not logged in, but when it is, the last item should become <li>Contul meu</li> in all the pages.
login.php, which is implemented in the login.html and signup.html pages
<?php
$link = new mysqli("localhost", "root", "", "graphicdesign");
if($link->connect_error){
die("ERROR: Nu s-a putut realiza conexiunea la baza de date " .$link->connect_error);
}
session_start();
$email =$_POST['email'];
$password=$_POST['pass'];
$result = $link->query("SELECT email_cl, parola_cl FROM clienti WHERE email_cl= '".$email."' AND parola_cl= '".$password."'");
if($result->num_rows == 0 ) {
echo "Datele nu corespund!";
}
else {
$_SESSION['logged in']=true;
$_SESSION['email']=$email;
echo "Login cu succes!";
echo "<script>setTimeout(\"location.href = '../account.html';\",1500);</script>";
}
?>
How can I make the server know all the time which the user is logged in?
And how can I tell the HTML pages when to update their navigation based on that state of logged in or not?
The only thing I thought about was to duplicate all the pages ( but they are too many ) and then implement some code to test if the user is logged in to the server can choose the choice with the right menu... but doesn't seem okay at all...
Thank you!
First, you need to rename all your pages from .html to .php. There is absolutely no difference between the two extension except for this: if the page ends in .php, then the PHP processor knows to interpret any PHP code found between <?php and ?> tags. If the page ends in .html then the PHP will not be processed. HTML will continue to work exactly the same. Try it now - create a test page with some HTML in it and name it with the .php extension. You will see it works exactly like one ending in .html
In order to change the navigation items after the user has logged in, you can refer to the $_SESSION variables that you set when they logged in. Note: do not use spaces in session variable names - underscore chars (eg logged_in) are fine, though.
Example:
<?php
if ($_SESSION['logged_in'] == true){
$out = '<li>Contul meu</li>';
}else{
$out = '<li>Contul meu</li>';
}
echo $out;
?>
And, most important - make sure that you put session_start(); at the top of every PHP file.
As a side note, I personally like to have a <?php ?> section at the top of my PHP files that contains as much of my PHP code as possible. For example, I would place the above code right up at the top of the file -- before any HTML -- before <!DOCTYPE html>. Then, I have all my HTML code and -- where it belongs -- I echo out the PHP variable, like this:
<li class="active">Acasă</li>
<li>login</li>
<?php echo $out ?>
<li>sign in</li>
(I intentionally placed your Contul meu menu item for DEMO purposes, so that you can see stuff above it and also below it. In your example, it was the last menu item which would not be as clear for demo purposes.)
you can display your menu based on condition. Put a check using session on the top of every login/logout and signin page like that:-
Note: you make your start session on top of every file.
<?php
session_start();
if(isset($_SESSION['email']) && !empty($_SESSION['email'])){
?>
Logout
<?php
}else{
?>
<li class="active">Acasă</li>
<li>login</li>
<li>sign in</li>
<li>Contul meu</li>
<?php } ?>
I'm trying to use a button to load more data from database with PHP.
Till now I can count the results and the results that are showing.
So when $count==$countAll, all the results are normally showing.
Can someone explain why this isn't working?
// Count all results
$allResults = $conn->prepare("SELECT*FROM tl_picture WHERE text LIKE '%$innerhtml%' ORDER BY id DESC");
$allResults->execute();
$countAll =$allResults->rowCount();
echo "Found results: ".$countAll."<br>";
//max 20 results showing
$statement = $conn->prepare("SELECT*FROM tl_picture WHERE text LIKE '%$innerhtml%' ORDER BY id DESC limit 20");
$statement->execute();
$collection = $statement->fetchAll();
$count =$statement->rowCount();
echo "viewable results: ". $count;
<script>
<?php if($count==$countAll): ?>
document.GetElementById('loadButton').style.display='none';
} else {
document.GetElementById('loadButton').style.display='block';
}
<?php endif; ?>
</script>
What you are trying to achieve is normally done with ajax calls. So the check for the visibility of the Load More button must be done with javascript after every asynchronous load. PHP runs on server side, once before sending the HTML document to your browser and doesn't help at all in this case.
Also you execute the sql query for all rows and then you do it again with limit this time. It is inefficient to run twice the same query just to get the number of all records.
The logic should be that you load 20 rows and each row should have an identifier. Using the identifier of the last loaded row, you ajax request the next 20 rows etc. If response data from ajax is empty, then you hide the Load More button (or disable it and set text to something like "No more records").
You might have an error in this script:
<script>
<?php if($count==$countAll): ?>
document.GetElementById('loadButton').style.display='none';
} else {
document.GetElementById('loadButton').style.display='block';
}
<?php endif; ?>
</script>
You might change it to something similar to:
<script>
<?php if($count==$countAll): ?>
document.GetElementById('loadButton').style.display='none';
<?php } else { ?>
document.GetElementById('loadButton').style.display='block';
<?php endif; ?>
</script>
The problem might be that your } else { is in JavaScript, yet there might be no if for it?
Currently, we have a PHP Form that uses an entry for a redirect to Private sites within our main website, of which code we use the following:
<?php
//Turn the content from text box into variable
$page=$_POST['text'];
if($_POST['text']=='sit') {
//set up a redirect to that page
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://example.com/index.php/private/sit\">";
}
else if($_POST['text']=='pony') {
//set up a redirect to that page
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://example.com/wp-content/uploads/sites/2/client/pony/index.html\">";
}
Originally, we had implemented a final line that if someone inserted something wrong, it would refresh the page:
else {
echo "<meta http-equiv=\"refresh\"content=\"0;URL=http://example.com\">";
}
?>
How to show a message that says "Error" in Bold and Red next to the form?
You can send a parameter in the URL you use to refresh. And then check on the parameter after the URL refresh by Javascript and add the error.
In this case you will echo something like this instead
echo "<meta http-equiv=\"refresh\"content=\"0;URL=http://example.com?error=1\">";
Then in your html, use Javascript or PHP to check on the URL param and render error with style.
I want to display logging user name. But below given code working perfect but the same page only working. I want home.php to display.
Login.php
if($check_user>0)
{
$_SESSION['user_name']=$username;
echo "<script>window.open('home.php','_self')</script>";
}
Home page coding
<?php echo($_SESSION['username']); ?>
But this code undefined index error showing.
How can call session username in home page
You have undefined index because you setting $_SESSION['user_name'] and try to read $_SESSION['username']. Change your home page code to: <?php echo($_SESSION['user_name']); ?>