Jump to content
Larry Ullman's Book Forums

Help! How Do I View Items In 3 X 3 Rows&Column In Shop.Php


Recommended Posts

There's two pieces to what you want. The first is to paginate the results, returning 9 at a time. That's fairly basic stuff, covered in my PHP & MySQL book, among other places. 

 

The second is to use a flag variable to know when to create a new row:

$location = 1;
$num_per_row = 3;
echo '<table>';
echo '<tr>';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    if ($location === $num_per_row) {
        // Close the previous row, start a new one, reset the counter:
        echo '</tr><tr>';
        $location = 1;
    }

    // Print the item:
    echo "<td>{$row['whatever']}</td>";
    
    // Increment the counter:
    $location++;
}

// Complete the last row with empty cells, just in case:
for ( ; $location < $num_per_row; $location++) {
    echo '<td> </td>';
}

That should do it. Again, you'll need to add pagination to this to show the next page of results.

Link to comment
Share on other sites

Hi Larry, thanks for the reply. I inserted six items to the category to see if it will display as 2 X 3 matrix but I still have it as one column. Please, any idea on what I'm not doing right, I have not included the pagination yet.It is not giving me any error. Here is my code:

 

<?php 
$location = 1;
$num_per_row = 3;
echo '<table>';
echo '<tr>';
 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { // Fetch each item.
 
    if ($location == $num_per_row) {
        // Close the previous row, start a new one, reset the counter:
        echo '</tr><tr>';
        $location = 1;
    }
    
// Print the item within some HTML:
echo '<li><h3>' . $row['category'] . ' </h3>
                <p><img alt="' . $row['category'] . '" src="/products/' . $row['image'] . '" />' . $row['description'] . '<br />
                <a href="/browse/' . $type . '/' . urlencode($row['category']) . '/' . $row['id'] . '" class="h4">View All ' . $row['category'] . ' Products</a></p>
             </li>';
             
             // Increment the counter:
    $location++;
}
 
// Complete the last row with empty cells, just in case:
for ( $location < $num_per_row; $location++) {
    echo '<td> </td>';
}
 
}
 
?> 
 

 

Link to comment
Share on other sites

// Print the item within some HTML:
echo '<li><h3>' . $row['category'] . ' </h3>
                <p><img alt="' . $row['category'] . '" src="/products/' . $row['image'] . '" />' . $row['description'] . '<br />
                <a href="/browse/' . $type . '/' . urlencode($row['category']) . '/' . $row['id'] . '" class="h4">View All ' . $row['category'] . ' Products</a></p>
             </li>';

 

Your problem is  down to your markup. The above code is outputting each row of data as list items which by default are block items. It appears that you don't have an understanding of HTML as you are not using the table based layout you have started. You may not be getting php errors but you will certainly get display errors as you haven't started and ended your list.

 

There are a number of ways to rectify your current situation e.g. open and close your list and use css to style the list items as inline. The easiest solution though not necessarily the best is to replace the list tags with table data tags.

Link to comment
Share on other sites

Hi Marg thanks. I have to be frank here. I am studying PHP and MySQL for Dynamics website (Fourth Edition) now after I got recommendation  from Effortless E-Commerce with PHP and My SQL to see if I can write PHP codes. I also have expert PHP and MySQL by Andrew Curiso et al.

 

I have not started learning HTML. Is it the basics for learning PHP and MySQL? Kindly recommend a nice book on it for me. I don't like confusing myself with many books on same subject. These two Larry's book is OK for a beginner like me on PHP and MySQL. I just wanted to tweak some things I see in other websites and get grounded on it.  

 

If you wouldn't mind, I want to display 24 items in each category in a 8 x 3 matrix , i.e. it will have 8 rows and 3 columns. If it exceeds 24 items let say 50 items, then the remaining will be displayed in other pages. I know I have to change stuffs in browse.php. sales.php, shop.php and some html files.  I will do all it takes to learn this. Just tell me what to do. I'm grasping this PHP stuff by Larry anyway. It is helpful for beginners like me. 

 

Thanks

 

Thanks

Link to comment
Share on other sites

The only requirement for a web page is HTML markup. It may not look great or do fun things but it is the language to display things on a web page.

 

CSS is used for styling - to dictate how the page will look - the colours, layout, fonts etc. Javascript is used to provide behavioural features such as sliders, rotators, drop down menus; though with clever use of CSS you can actually provide some of these features without javascript. PHP and MySQL make the page dynamic by manipulating stored data and responding to user driven events such as submitting a form. But none of these will display a web page.

 

There are a number of online tutorial sites you can try - lynda.com, htmldog, codeacademy are a couple and there are some good resources on this page.

 

Below is a table based layout. For the pagination, you will need to keep track of the rows as well. There is an example in Larry's PHP and MySQL book that you can refer to.

<?php 
$location = 1;
$num_per_row = 3;
echo '<table>';
echo '<tr>';
 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { // Fetch each item.
 
    if ($location == $num_per_row) {
        // Close the previous row, start a new one, reset the counter:
        echo '</tr><tr>';
        $location = 1;
    }
    
// Print the item within some HTML:
 echo '<td><h3>' . $row['category'] . ' </h3>
 <p><img alt="' . $row['category'] . '" src="/products/' . $row['image'] . '" />' . $row['description'] . '<br />
 <a href="/browse/' . $type . '/' . urlencode($row['category']) . '/' . $row['id'] . '" class="h4">View All ' . $row['category'] . ' Products</a></p>
 </td>';
             
  // Increment the counter:
  $location++;
  }
 
  // Complete the last row with empty cells, just in case:
 for ( $location < $num_per_row; $location++) {
    echo '<td> </td>';
   }
}
?> 
  • Upvote 2
Link to comment
Share on other sites

  • 2 weeks later...

Hi All, sorry for being away for a while, just came back. Marg! Thanks. It is working now. However, I tried working on the pagination stuff following Larry's book on page Ch10, pg 316 but it is giving me " Commands out of sync; you can't run this command now." Here is my modified code.

 

 

<?php // This page is included by browse.php.
// This page displays the available coffee products.
// This page will make use of the query result $r.
// The query returns an array of: description, image, sku, name, and stock.
// This is the second version of the script, which handles the product's avaialability in a better manner.

// Only display the header once:
$header = false;

// Added later in Chapter 8:
include ('./includes/product_functions.inc.php');

$display = 2;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric
($_GET['p'])) { // Already been determined.

$pages = $_GET['p'];

}

else { // Need to determine.

// Count the number of records:
$q = "SELECT COUNT(id) FROM non_coffee_products";
$r = @mysqli_query ($dbc, $q);
$row = @mysqli_fetch_array ($r,MYSQLI_NUM);
$records = $row[0];

// Calculate the number of pages...
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display);
} else {
$pages = 1;
}

} // End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric
($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

//Define the query:
$q = "SELECT id, name, image, description, price AS id, name, image, description FROM non_coffee_products  ORDER BY price ASC LIMIT $start, $display";

//Run the query
$r = @mysqli_query ($dbc, $q);


$location = 1;
$num_per_row = 3;
echo '<table>';
echo '<tr>';
// Loop through the results:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

if ($location == $num_per_row) {
        // Close the previous row, start a new one, reset the counter:
        echo '</tr><tr>';
        $location = 1;
    }

// If the header hasn't been shown, create it:
if (!$header) { ?>
  <!-- box begin -->
  <div class="box alt">
   <div class="left-top-corner">
      <div class="right-top-corner">
         <div class="border-top"></div>
        </div>
     </div>
     <div class="border-left">
      <div class="border-right">
              <div class="inner">
                 <h2><?php echo $category; ?></h2>
                
                    <p><img alt="<?php echo $category; ?>" src="/ecom/html/products/<?php echo $row['g_image']; ?>" /><br><?php echo $row['g_description']; ?></br></p>
               
             </div>
          </div>
     <div class="left-bot-corner">
      <div class="right-bot-corner">
         <div class="border-bot"></div>
        </div>
     </div>
  </div>
  <!-- box end -->

<?php // The header has now been shown:
  $header = true;
} // End of $header IF.

// Show each product:
echo '<td><h3>' . $row['name'] . '</h3>
  
      <p><img alt="' . $row['name'] . '" src="/ecom/html/products/' . $row['image']  . '" />'.'<br>'  . $row['description'] . '<br />
  <strong>Price:</strong> ₦' . $row['price'] . '<br /> 
  <strong>Availability:</strong> ' . get_stock_status($row['stock']) . '</p>
    <p><a href="/cart.php?sku=' . $row['sku'] . '&action=add" class="button">Add to Cart</a></p></td>';
   // Increment the counter:
  $location++;
  }

  // Complete the last row with empty cells, just in case:
for ( ; $location < $num_per_row; $location++) {
    echo '<td> </td>';
   
} // End of WHILE loop.
echo '</table>';

mysqli_free_result ($r);
mysqli_close($dbc);

// Make the links to other pages, if necessary.
if ($pages > 1) {

// Add some spacing and start a paragraph:
echo '<br /><p>';

// Determine what page the script is on:
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous link:
if ($current_page != 1) {
echo '<a href="browse.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
}

// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '<a href="browse.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}

} // End of FOR loop.

// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo '<a href="browse.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
}

echo '</p>'; // Close the paragraph.

} // End of links section.

?>      
 

Link to comment
Share on other sites

 Share

×
×
  • Create New...