show all users from table by pagination - php

i got large amount of users into my table and i want to made an small script to show all users as 10 per page
I'm using this script http://www.phpeasystep.com/phptu/29.html but its showing only 1 result per page.
As they are saying on pic 7 i loop the code to show the users as this :
$id = $row["id"];
$username = $row['username'];
$date = $row['date'];
And its echo-ed as this:
<?php
echo '<div class="content">
<h1> ' . $username . ' </h1>
<div class="content_item">
<p>'.$date.'</p>
</div>
</div>';
?>
However i dont understand why its showing only 1 result per page!
I setted $start as 0 and $limit as 10

You have to place this echo code in a Loop, which would echo the Details of Users repeatedly.
For Example-
foreach($results as $r)
{
echo $r->id;
echo $r->username;
}

Related

PHP linking from one page to the next

I am trying to create a small website to sell items on and learn php. I have a index.php which links to a chairs.php page and then from there to a viewItem.php page depending on which item they choose.
I have in my database 4 items. On the chairs.php a while loop runs to display on the page all items in that database.
I would like to use the id of the item to link to the next page but I think the while loop is always going to assign to the variable that I am using the very last entry in that while loop.
Can someone point me in the direction of how I would resolve this? I would like the user to select a Chair 1 on my Chairs.php page with ID 1 in my db to link to the corresponding page. Currently all links on the chairs.php would link to the last item in my while loop which is then assigned to the $_SESSION['output_id'] = $output_id variable.
Thank You
<?php
$query = "SELECT * FROM `chairs_table";
$queryChairs = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($queryChairs)) {
$output_id = $row["ID"];
$output_img = $row["Image"];
$output_name = $row["Name"];
$output_desc = $row["Desc"];
$output_price = $row["Price"];
$_SESSION['output_id'] = $output_id;
$_SESSION['output_img'] = $output_img;
$_SESSION['output_name'] = $output_name;
$_SESSION['output_desc'] = $output_desc;
$_SESSION['output_price'] = $output_price;
?>
<div class="itemContainer" onclick="location.href = 'viewItem.php?id=<?php echo "{$output_id}" ?>'" style="cursor:pointer;" >
<div> <img src = "img/<?php echo "{output_img}" ?>"> </div>
<div class="itemTextTitle"><?php echo"{$output_name}" ?></div>
<div class="itemTextDesc"><?php echo"{$output_desc}" ?></div>
<div class="itemTextPrice">cop <?php echo"{$output_price}"?></div>
</div>
<?php
}
?>
</div>

I want to display products from my database to HTML. I only get the last row from my database to show up

I am working on a school project, a simple webshop.
I decided to ask it here.
What i want to do is display products from my database to my "product page" using PHP.
For some reason i only get the last product to show, also having trouble with the images.. I know the query is correct.
"SELECT * FROM products";
Here is my code.
This is my PHP, I have put this on top of the product.php page.
product.php
<?php
session_start();
include "includes/connectie.php";
include "includes/navbar.php";
$vraag = "SELECT * FROM products";
//var_dump($vraag); Var dump test
$resultaat = $conn->query($vraag);
if ($resultaat->num_rows > 0)
{ // Min. 1 row returned
while ($rij = $resultaat->fetch_assoc())
{
$id = $rij['id'];
$titel = $rij['product_name'];
$prijs = $rij['product_price'];
$omschrijving = $rij['description'];
$foto = $rij['image'];
}
}
else
{
// Do nothing
}
?>
This is my HTML code below, where i want to "display" i also have this in the same file as code above. So first php, then html
product.php
<body>
<div class="products">
<div class="productContainer">
<h1 id="banner">ALL ITEMS</h1>
<div class="productRow">
<?php
echo '<img src="img/product_'.$id.'.jpg">';
?>
</div>
<?php
echo '<h1>'.$titel.'</h1>';
echo '<p class="prijs">'.$prijs.'</p>';
echo '<p class="omschrijving">'.$omschrijving.'</p>';
?>
<div class="productRow">
<?php
echo '<img src="product_'.$id.'.jpg">';
echo '<h1>'.$titel.'</h1>';
echo '<p class="prijs">'.$prijs.'</p>';
echo '<p class="omschrijving">'.$omschrijving.'</p>';
?>
So i only put the HTML/PHP where i would want the first two products as example.
In total i have 6 products. I also included a picture of the database
database
What am i doing wrong?
This is how it looks right now:
example of product display
in your php file you have added all the values from database to an array. but you have not used any loop when you are trying to display array values in html. loop is a must to display all the values of array. Add a loop in your HTML file.

How to create page numbers (for navigation) on a web page

I am writing PHP script to display data from the database (Mysql) on my webpage. I don't want all the information to display on a single page to avoid scrolling. However, I want to display only a few and then use page numbers to navigate to the rest. I managed to create the page numbers and yet still all the information is showing on a single page. Below is my code:
<?php
include('./includes/DB_Config.php');
$status = 1;
// Set number of Post per page for navigation
$post_per_page = 2;
if (isset($_GET['page']))
{
$startP = ($_GET['page'] - 1) * $post_per_page;
}
else
{
$startP = 0;
}
$post = mysqli_query ($mySQL_Conn, "SELECT * FROM BlogPost WHERE status = '$status'");
// Fetch Data or number of rows
$dataRw = mysqli_num_rows($post);
$pages = $dataRw / $post_per_page;
if ($dataRw == 0)
{
$_SESSION['NoPost'] = "No Post Found!";
}
else
{
unset($_SESSION['NoPost']);
//$pages = ceil($dataRw / $rows_per_page);
//$pages = array_slice($dataRw, $startP, $post_per_page);
while ($data = mysqli_fetch_array($post)) //Fetch data in array
{
//Assign data to variabes
$name = $data['blogger_Name'];
$postDate = $data['dateTime'];
$blogpost = $data['blog_message'];
$date = strtotime($postDate);
?>
<p> <span><?php echo $_SESSION['NoPost'] ; ?> </span></p>
<p> <span>By:</span><?php echo $name; ?> </p>
<p> <span>Posted On: </span> <?php echo date("j F Y", $date); ?> </p>
<p> <span>Post: </span><?php echo $blogpost; ?> </p>
<?php
}
}
?>
<hr>
<?php
for ($currentpage = 0; $currentpage < $pages; $currentpage++ )
{
?>
<span> <?php echo $currentpage + 1; ?> </span>
<?php
}
?>
What you are trying to accomplish is a very common pattern, and it's called pagination.
You can use LIMIT and OFFSET in your mysql queries to get only a certain subset of the data, and you can use this for basic pagination.
For example, if you want to display 50 results per page, and the user is on the third page, you set LIMIT to 50 and OFFSET to 2*50 (100), keep in mind the first page is an offset of 0 so we subtract the page by one.
You can use the following sql query to get a subset of data for a specific page.
"SELECT * FROM Users LIMIT $num_results OFFSET ".(($page_num-1)*$num_results);
You can get the total number of possible pages by getting the total row count from the Users table and dividing it by $num_results. A good idea might be to have your output script take a GET url query parameter that will be used as the page number.

Get the ID of my clicked product

I'm new to this site and wondering if somebody could help. I'm creating a website using bootstrap. I added some products in my DB and have used a while loop to display them on the page.
If you click on one of the products it takes you too a new page which should display all the information from only that product.
if ($select_db) {
$SQL = "SELECT * FROM products";
$result = mysql_query($SQL);
?>
<?php
$c = 0;
$id = -1; // The counter
$n = 3; // Each Nth iteration would be a new table row
while ($db_field = mysql_fetch_assoc($result)) {
$itemName = $db_field['name'];
$itemDescription = $db_field['description'];
$itemPrice = $db_field['price'];
$myPic = $db_field['image_name'];
if ($c % $n == 0 && $c != 0) { // If $c is divisible by $n...
echo '<div class="row" ></div>';
}
$c++;
$id++;
?>
<div class="col-md-4 col-sm-4" style="background-color:lavender;" id = 1>
<a href="productInfo.php">
<p> <?php echo $c ?> </p>
<h2> <?php echo $itemName ?> </h2>
<p><img class="img-responsive" img src= '<?php echo $myPic ?>' alt="Oops, Image cannot be found!" height="300" width="300"/></p>
<h3><?php echo $itemDescription ?></h3>
<p><?php echo $itemPrice ?></p>
<div id="selector" class="btn-group">
<button type="button" class="btn btn-primary" id="<?php $id ?>">Add</button>
</div>
<?php
$productsArray[] = array(
"id" => $id,
"itemName" => $itemName,
"itemDescription" => $itemDescription,
"price" => $itemPrice
);
//$_SESSION['sessionArray']=$productsArray;
?>
</a>
</div>
<?php
Now when I click on one of the columns it takes me to productInfo.php. I'm stuck as to how to display the product that is clicked?
I have added a variable $id which is the same as the array, example array [0] has id of 0, array [1] has id of 1. I think I'm trying to say if ID = 0 then display the results in the array at place [0]. I'm not sure if this helps?
I tried to display just the $id for now on productInfo.php but it only shows the last $id in the array
<?php
session_start();
if (isset($_SESSION["id"])) {
$newID = $_SESSION["id"];
echo $newID;
}
?>
I know this because it doesn't know which one I'm selecting. Am I making this too complicated?
Any help would be appreciated greatly!
Within your while loop I would add:
View More
This would concatenate the value of $id++ into the href under each item. Then all you need to do is create a landing page for product_page.php, define the $id on that page again and pull the data for that product from the db. You can do this without using any arrays.
Edit:
You would define $id on your product_page.php using $_GET['id'], as it is in the url, supplied by your href above ^
A good practice to get into whilst in the development stages would be to echo $id; to see if it is echoing the correct data from your while loop. If it echo's the correct $id then you can send the $id to the url through the href.
Let me know if this works or not.
Pulling data from db of your ID:
$sql = "SELECT * FROM table WHERE id='".$myID."'";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$data1 = $row['whatever'];
echo $data1;

only the first image I am bringing from database is appearing

I'm having a strange error, and I know Im posting a lot of code, but, Im really not understanding what Im doing wrong, so I decided to put all the code here related to my problem.
I also commented my code to be easily to understand context.
The problem is:
Im bringing my news information from database, and Im having a problem with images, only the image of the first post, which is the recent news, is appearing, the others dont appear.
However if I delete the code that I quoted like FROM HERE* **TO HERE*, images appear correctly.
Someone there realize why this is happening in this code excerpt that causes the images of my news do not appear?
<div id="content">
<h1>News</h1>
<?php
//first I read my news
$readNews = $pdo->prepare("SELECT * FROM news WHERE status = 1 ORDER BY date DESC LIMIT 0,4");
$readNews->execute();
//while exist news I want to read category of that news, and show news info in my article
while ($readNewsResult = $readNews->fetch(PDO::FETCH_ASSOC))
{
$readCat= $pdo->prepare("SELECT * FROM categories , news WHERE id_cat = ?");
$readCat->bindValue(1,$readNewsResult['category']);
$readCat->execute();
$resultReadCat = $readCat->fetch(PDO::FETCH_ASSOC);
//I show news info in my article
echo '<article class="loop-news">';
echo '<img src="'.$path.$readNewsResult['thumb'].'" />';
echo '<h2>'.$readNewsResult['title'].'<br /></h2>';
echo '<span>'.$resultNewsResult['date'].'</span>';
echo '<p>'.$readNewsResult['content'].'<a id="'.$readNewsResult['id_not'].'" class="fancybox" href="#show'.$readNewsResult['id_not'].'">See more</a></p>';
//I repeat my news info to show inside fancybox
echo '<div class="show-container">';
echo '<div id="show'.$readNewsResult['id_news'].'">';
echo '<h2>'.$readNewsResult['title'].'</h2>';
echo '<span>'.$resultNewsResult['date'].'</span>';
echo '<img src="'.$path.$readNewsResult['thumb'].'" />';
echo '<p>'.$readNewsResult['content'].'</p>';
//******************FROM HERE***************************/
//I read my table links_news to see if news that is reading have links associated
$readLinks = $pdo->prepare("SELECT * FROM links_news WHERE news_id = ?");
$readLinks->bindValue(1,$readNewsResult['id_news']);
$readLinks->execute();
//if current reading news have links it will show on my links div
echo '<div class="links">';
echo '<h3>Relative Links:</h3>';
echo '<ul class="links1">';
while ($readLinksResult = $readLinks->fetch(PDO::FETCH_ASSOC))
{
echo '<li> '.$readLinksResult['link'].'</li>';
}
echo '</ul>';
echo '</div>';<!-- close class links-->
//****************TO HERE*******************/
echo '</div>';<!-- close class show -->
echo '</div>';<!-- close class show-container-->
echo '</article>';
}
?>
</div>

Categories