Forum Moderators: coopster

Message Too Old, No Replies

HELP! table, mysql results, rows and colums, ideas?

php, table, mysql, result,

         

diegomh7

8:09 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



Hi guys, could you please help me sort out this problem,

I need to echo the reaults from a database onto a page, the results need to be displayed in rows and colums, ie 20 products in the database = 4 columns with 5 rows,

the hard part is the while loop to load the data in and dynamically gernerate a html table to put the data in, i want the page to look like this <product picture with price and desc underneath in a 3 wide layout - jatar_k> but i want it to be created dynamically from the database and im stuck on the while loop, something like this :

<?php

mysql_connect("You know how, do you?");
mysql_select_db("db..");

$query = mysql_query("SELECT * FROM your_products_table ORDER BY some_key ASC");
echo "<table>\r\n <tr>";
while($arr = mysql_fetch_array($query)) {
if($i == 3) { // For every four column do new line.
echo "</tr><tr>\r\n";
}
echo "\t<td>";
echo $arr["some_row"];
echo "</td>\r\n";
$i++;
}
?>

any ideas guys? pleaasse help me out i need this by tomorrow and im runing outta time,

thanx in advance

Diegomh7

[edited by: jatar_k at 8:11 pm (utc) on Jan. 12, 2006]
[edit reason] no urls thanks [/edit]

jatar_k

8:20 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



a row (in the html table) should look like this

<tr>
<td><p><img src="path/to/pic.jpg"><br>product name<br>product price</td>
<td><p><img src="path/to/pic.jpg"><br>product name<br>product price</td>
<td><p><img src="path/to/pic.jpg"><br>product name<br>product price</td>
</tr>

a dynamic table script I have posted here a few times is

$sql = "select * from sometable"; 
$result = mysql_query($sql);
$tdcount = 1;
$numtd = 3; // number of cells per row
echo "<table>";
while($row = mysql_fetch_array($result)) {
if ($tdcount == 1) echo "<tr>";
echo "<td>some stuff: $tdcount</td>"; // display as you like
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";

you should be able to use that to adapt to your layout. I don't want to do all the work ;)

diegomh7

8:44 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



ROCK ON JAKAR ROCK ON!

Im gonna have a go and see how many times my head can hit the screen before i pass out and post again :)

thanx buddy

Diegomh7

jatar_k

8:48 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just replace this

echo "<td>some stuff: $tdcount</td>"; // display as you like

with a bunch of echo's for your actual display (you only need a single cell)

<td><p><img src="path/to/pic.jpg"><br>product name<br>product price</td>

replace the strings in that example with the variables you are pulling from the db

no problem ;)

diegomh7

9:21 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



one last thing, where am i going wrong

i want to use an alt tag for the image which is the prod_name from the database, and also the price but i normally break the php code for the html bits and im confusing myself with the '' instead of the "", do i still need to

$prod_name = mysql_result($result,0,'prod_name');

or does fetch_array do that for me?

look

<?php
$tdcount = 1;
$numtd = 3; // number of cells per row
echo "<table>";
while($row = mysql_fetch_array($result)) {
if ($tdcount == 1) echo "<tr>";
echo "<td><p><img src='path/to/pic.jpg' alt='$prod_name' 'width='212' height='247' /><br>$prod_name<br>£ $price $tdcount</td>"; // display as you like
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";

?>

thanx

diegomh7

9:48 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



kinda like this?

//Defined Variables
$rs = "select * FROM anula_footwear order by id";
$result = mysql_query($rs);
$num = mysql_numrows($result);

<?php
$tdcount = 1;
$numtd = 3; // number of cells per row
echo "<table>";
while($row = mysql_fetch_array($result)<$num) {
$i=0;
$prod_name = mysql_result($result,$i,'prod_name');
$price = mysql_result($result,$i,'price');



if ($tdcount == 1) echo "<tr>";
echo "<td><p><img src='path/to/pic.jpg' alt='$prod_name' 'width='212' height='247' /><br>$prod_name<br>£ $price $tdcount</td>"; // display as you like
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
$i++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";

?>

jatar_k

9:54 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



let's change this line a little

echo "<td><p><img src='path/to/pic.jpg' alt='$prod_name' 'width='212' height='247' /><br>$prod_name<br>£ $price $tdcount</td>"; // display as you like

we can get rid of my comment and the $tdcount var

echo "<td><p><img src='path/to/pic.jpg' alt='$prod_name' 'width='212' height='247' /><br>$prod_name<br>£ $price</td>";

I am guessing this doesn't display anything?

are these the actual column names in your db?

price
prod_name

<added>we posted at the same time, answer my questions and I can explain a few things that should help you, then I will remove some lines from your post above and see if we can't get this working. ;)

diegomh7

10:07 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



yeah they are columns in the table, i also need the table to get the id so that when the user click the image it goes to a new page and send the image id to a more detailed view, anyway

<?php
$tdcount = 1;
$numtd = 3; // number of cells per row
echo "<table>";
while($row = mysql_fetch_array($result)) {

//NEW BIT
$i = 0;
$id = mysql_result($result,$i,'id');
$prod_desc = mysql_result($result,$i,'prod_name');
$price = mysql_result($result,$i,'price');

//END OF NEW BIT, STILL DOESNT WORK PROPERLY

if ($tdcount == 1) echo "<tr>";
echo "<td><p><a href='detailed_product_view_footwear.php?id=$id'><img src='path/to/pic.jpg' alt='$prod_name' 'width='212' height='247' /></a><br>$prod_name<br>£ $price $tdcount</td>";

if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;


//ANOTHER NEW BIT
$i++;

//END OF NEW BIT

thanx in advance

diegomh7

[edited by: jatar_k at 10:34 pm (utc) on Jan. 12, 2006]
[edit reason] no urls thanks [/edit]

diegomh7

10:22 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



IVE DONE IT LOOK LOOK

<?php
$tdcount = 1;
$i = 0;
$numtd = 3; // number of cells per row
echo "<table>";
while($i<$num) {

//NEW BIT

$id = mysql_result($result,$i,'id');
$prod_name = mysql_result($result,$i,'prod_name');
$price = mysql_result($result,$i,'price');

//END OF NEW BIT, STILL DOESNT WORK PROPERLY

if ($tdcount == 1) echo "<tr>";
echo "<td><p><a href='detailed_product_view_footwear.php?id=$id'><img src='path/to/pic.jpg' alt='$prod_name' 'width='212' height='247' /></a><br>$prod_name<br>£ $price $tdcount</td>";

if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;


//ANOTHER NEW BIT
$i++;

//END OF NEW BIT

}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";

?>

thanx for all your help JAKAR

regards,

diegomh7

diegomh7

10:25 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



no no not right yet,

its repeating the last entry on the row onto the row below.

last thing,

now its done

if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;

//THIS IS NEW $i++;

} else {
$tdcount++;

$i++;

if anyone else want to do something like this dont forget to add the

$num = mysql_numrows($result);

at the top of the page

jatar_k

10:41 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



here check this one out

I removed your while loop and put it back to the mysql_fetch_array, it will save quite a bit of code. You don't need to do the multiple mysql_result as this

while($row = mysql_fetch_array($result)) {

pulls the next available row into an array which is called $row. You can then access these values by using the column name and a little array syntax

<?php
$rs = "select * FROM anula_footwear order by id";
$result = mysql_query($rs);

$tdcount = 1;
$numtd = 3; // number of cells per row

echo "<table>";
while($row = mysql_fetch_array($result)<$num) {

if ($tdcount == 1) echo "<tr>";
echo "<td><p><a href='detailed_product_view_footwear.php?id=" . $row['id'] . "'><img src='path/to/pic.jpg' alt='" . $row['prod_name'] . "' 'width='212' height='247' /></a><br>" . $row['prod_name'] . "<br>£ " . $row['price'] . "</td>";

if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";

?>

do you see how that works?

the only thing left is to get the actaul image in the cell instead of
<img src='path/to/pic.jpg'

;)