www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP product listing problem :/

  1. #1
    Join Date
    May 2014
    Posts
    3

    PHP product listing problem :/

    i will try to explain my problem but my English is bad so i hope you will understand me.

    Currently, when i add products in admin page they are displayed one next to other in one line, for example, if I add 10 products i have to scroll page to the right to see them all.

    Now I want to display them in a grid of 5 products. So, when i add products, five of them are displayed in one line, then the sixth product is displayed underneath the 1st product and so on. I really need this fast, so every help would be useful, and thank you for every answer. I am complete begginer in php so i don't undrestand much :/ Or if somebody have better idea how could i do this that also will be useful. On one site they told me to this :

    declare $count=1; out side while loop

    use like this

    if($count % 5==0) {
    echo "</tr> <tr>";
    }
    and before end of while loop increment $count++
    but i even cant' make that work, or i don't put in my code that right

    here is my code made with some tutorial videos

    PHP Code:
    <?php 
    include "storescripts/connect_to_mysql.php"
    $sql mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 10");
    $productCount mysql_num_rows($sql); // count the output amount
    if ($productCount 0) {
    echo
    "<table width='100%' border='0' cellspacing='5' cellpadding='6'>";
       echo 
    "<tr>";
    while(
    $row mysql_fetch_array($sql)){ 
            
    $id $row["id"];
    $product_name $row["product_name"];
    $price $row["price"];
    $date_added strftime("%b %d, %Y"strtotime($row["date_added"]));

        
    $count 0;   
        if(
    $count == 5) {
                
    $count 0;                     
                echo 
    "</tr> <tr>";       
        }
         echo 
    "<td width='17%' valign='top'><a href='product.php?id=" $id "'><img              `enter code here`style='border:#666 1px solid;' src='inventory_images/" $id ".jpg' `enter code here`alt='" $product_name "' width='77' height='102' border='1' />     </a>`enter code here`</td>";
           echo 
    "<td width='83%' valign='top'>" $product_name "<br />
           $" 
    $price "<br />";
           echo 
    "<a href='product.php?id=" $id "'>View Product Details</a></td>"

    }

    }else {
    echo 
    "We have no products listed in our store yet";
    }

       echo
    "</tr>";
     echo
    "</table>";
    mysql_close();
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    The initialization of $count needs to happen before you start the while() loop that fetches your results, and you need to remember to increment it inside of that loop. That could even be done as part of the if() condition. I'd do it this way:
    PHP Code:
    $numColumns 5// just change this to change the break point
    $count 0;
    while(
    $row mysql_fetch_array($sql)){
        
    // do your output, then...
        
    if(++$count $numColumns == 0) { // pre-increment instead of post-increment it
            
    echo "</tr>\n</tr>";
        }
    }
    // $count now represents how many results were displayed 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2014
    Posts
    3
    Quote Originally Posted by NogDog View Post
    The initialization of $count needs to happen before you start the while() loop that fetches your results, and you need to remember to increment it inside of that loop. That could even be done as part of the if() condition. I'd do it this way:
    PHP Code:
    $numColumns 5// just change this to change the break point
    $count 0;
    while(
    $row mysql_fetch_array($sql)){
        
    // do your output, then...
        
    if(++$count $numColumns == 0) { // pre-increment instead of post-increment it
            
    echo "</tr>\n</tr>";
        }
    }
    // $count now represents how many results were displayed 
    This works, thank you very much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles