NYCPHP Meetup

NYPHP.org

[nycphp-talk] Help in creating multiple columns and rows to display product name pice and images

Brent Baisley brenttech at gmail.com
Sat Dec 15 16:58:52 EST 2007


I have a class I created that I have been using for years to do  
almost exactly what you are looking to do. Although you don't really  
need to use PHP5 syntax to do it. I've attached the class file and an  
example file that shows how it works. The example is a calendar and  
it generates it's own example data array, but any array will work.  
Just put the files in a web accessible directory and call the  
listMaker.example.php file. This is just an example, so there are a  
number of things I am not doing, like sanitizing the form input data.

There are 3 essential settings to it:
1. The template for the data. In your case, it's what is in your  
while loop without the "tr"
2. The data, which is a multi-dimensional associative array. It's the  
array you get when you retrieve data from mysql the way you are.
3. The row delimiter. In your case "</tr><tr>"

When you call the makeList function, you tell it how many columns you  
want. It then creates however many rows are necessary based on the  
size of the data array. So if your data array has 9 items and you  
specify 3 columns, it will create 3 rows.

I did not mention pagination, because you should only be retrieving  
enough records from the database to build the page, not all the data.  
You would do this by adding a "LIMIT" clause to your query. There was  
a pagination discussion not too long ago, you should search the  
archives for that discussion.

Hope that helps.

Brent

On Dec 11, 2007, at 12:16 PM, anang tawiah wrote:

>
> I am a relative php amateur who is trying to kind of master the php  
> 5 syntax and I have using Larry Ulmanns php and mysql 2nd edition  
> book.  The problem I am facing is that in the authors example he  
> lists all the results.
>
>
>
> What I want to do for this project is to display the following  
> product information in rows and columns whereby;
>
>
>
> i could  specify the number of columns and rows per page (using php  
> 5 syntax)
> And finally I also want to paginate the result set (using php 5  
> syntax)
>
>
> I know this might sound trivial to most of you ‘Gurus’ on this  
> list (which is why I finally decided to stop trying to figure it  
> out myself and ask for help) and I appreciate your help in advance.
>
>
>
>
>
>
>
> <?php # Script 14.6 - browse_prints.php
>
> // This page displays the available prints (products).
>
>
>
> // Set the page title and include the HTML header.
>
> $page_title = 'Browse the Prints';
>
> include ('./includes/header.html');
>
>
>
> require_once ('../mysql_connect.php'); // Connect to the database.
>
>
>
> // Are we looking at a particular artist?
>
> if (isset($_GET['aid'])) {
>
>             $aid = (int) $_GET['aid'];
>
>             if ($aid > 0) {
>
>                         $query = "SELECT artists.artist_id,  
> CONCAT_WS(' ', first_name, middle_name, last_name) AS name,  
> print_name, price, description, print_id FROM artists, prints WHERE  
> artists.artist_id = prints.artist_id AND prints.artist_id =$aid  
> ORDER BY prints.print_name";
>
>             } else {
>
>                         $query = "SELECT artists.artist_id,  
> CONCAT_WS(' ', first_name, middle_name, last_name) AS name,  
> print_name, price, description, print_id FROM artists, prints WHERE  
> artists.artist_id = prints.artist_id ORDER BY artists.last_name  
> ASC, prints.print_name ASC";
>
>             }
>
> } else {
>
>             $query = "SELECT artists.artist_id, CONCAT_WS(' ',  
> first_name, middle_name, last_name) AS name, print_name, price,  
> description, print_id FROM artists, prints WHERE artists.artist_id  
> = prints.artist_id ORDER BY artists.last_name ASC,  
> prints.print_name ASC";
>
> }
>
>
>
> // Create the table head.
>
> echo '<table border="0" width="90%" cellspacing="3" cellpadding="3"  
> align="center">
>
> <tr>
>
> <td align="left" width="20%"><b>Artist</b></td>
>
> <td align="left" width="20%"><b>Print Name</b></td>
>
> <td align="left" width="40%"><b>Description</b></td>
>
> <td align="right" width="20%"><b>Price</b></td>
>
> </tr>';
>
>
>
> // Display all the prints, linked to URLs.
>
> $result = mysqli_query ($dbc, $query);
>
> while ($row = mysqli_fetch_array ($result, MYSQL_ASSOC)) {
>
>
>
>             // Display each record.
>
>             echo "   <tr>
>
>                         <td align=\"left\"><a href= 
> \"browse_prints.php?aid={$row['artist_id']}\">{$row['name']}</a></td>
>
>                         <td align=\"left\"><a href=\"view_print.php? 
> pid={$row['print_id']}\">{$row['print_name']}</td>
>
>                         <td align=\"left\">{$row['description']}</td>
>
>                         <td align=\"right\">\${$row['price']}</td>
>
>             </tr>\n";
>
>
>
> } // End of while loop.
>
>
>
> echo '</table>'; // Close the table.
>
>
>
> mysqli_close($dbc); // Close the database connection.
>
> include ('./includes/footer.html');
>
>
>
> ?>
>
>
>
>
>
>
>
> Thanks,
>
>
>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071215/d5bff0ff/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: listMaker.class.php
Type: text/php
Size: 5765 bytes
Desc: not available
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071215/d5bff0ff/attachment.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071215/d5bff0ff/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: listMaker.example.php
Type: text/php
Size: 4535 bytes
Desc: not available
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071215/d5bff0ff/attachment-0001.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071215/d5bff0ff/attachment-0002.html>


More information about the talk mailing list