...

View Full Version : PHP mysql_query



Jon W
03-24-2009, 04:33 AM
Okay, so I have a question and I'm wondering and how this would be done.

So I already know how to query stuff from the database, I know how to update, delete.... And all of the fun stuff. But one of the things that I've been wondering is how would I when I loop the data from the database get it to do in a orderly fashion..

example: Say I want it to do something like this...

<table align="center">



//so I know I'd make a while for or foreach but.. I need to order so many right
//then </tr> and start a new line

while($results = mysql_fetch_assoc($query)) {

echo "<tr>";
echo "<td>" . $results['name'] . "</td>"; // 1
echo "<td>" . $results['name'] . "</td>"; // 2
echo "<td>" . $results['name'] . "</td>"; // 3
//now </tr>
echo "</tr>";

}


</table>

You get the idea.. But normally you could only do something like this




while($results = mysql_fetch_assoc($query)) {

echo "<tr>";
echo "<td>" . $results['name'] . "</td>"; //1
echo "</tr>";

}


Just because a loop like that wouldn't work.. So how is this done?

Thanks,
Jon W

steelaz
03-24-2009, 04:48 AM
Whats is exactly you're trying to achieve? You want to print 3 columns of names and then start a new line?

ShaneC
03-24-2009, 05:07 AM
Hooray for the for loop! I'd recommend just using a nice i variable to make your <td> breaks.

Alright, let's say you've got a DB structure like this:



|---carID---|-----carName-----|-----carColor-----|
|-----------|------------------|------------------|
|----1------|------Civic------- |-----black -------|
|----2------|------Mustang----|-----red----------|
|----2------|------Lotus-------|-----yellow-------|
I could then implement a while loop such as the one below to print all the results out:



<?php

// Open the connection to your database
openDatabaseConnectionFunction();

// Grab your data using a mysql_query
$sql = mysql_query( "SELECT * FROM cars_table ORDER BY `carID` ASC" );

// Let's use a classic incremental variable, i
$i = 0;

// Now, so we don't create a new <tr> if the table has ended
$totalEntries = mysql_num_rows( $sql );

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

// Let's reduce the total entries to the number after this loop instance
$totalEntries--;

// Create a new table if the increment is back at the beginning so long as there are more entries
if( $i == 0 && $totalEntries > 0 ){

echo "<tr>\n";

}

if( $totalEntries <= 0 ){

// If there are no more entries after this loop, close off the table.
echo "<td>" . $row['carName'] . "</td>\n";
echo "</tr>\n";

}else{

// If there are more entries then just print the entry without closing the table
echo "<td>\n" . $row['carName'] . "</td>\n";

}

// If we're at the end of a block, let's start a new one
if( $i == 3 ){

// Close up the table and then start a new one
echo "</tr>\n";
$i = 0;

}else{

// Increment $i
$i++;

}

}

?>
That should then print:



<tr>
<td>Civic</td>
<td>Mustang</td>
<td>Lotus</td>
</tr>

Jon W
03-24-2009, 05:34 AM
So just that I'm understanding this. when you say...



$totalEntries--;


That is the same as



$totalEntries =='';


Right?

ShaneC
03-24-2009, 06:10 AM
No, $totalEntries--; has the opposite effect of $i++;. Essentially, with ++ you are adding one to that variable, with -- you are subtracting one.

So if $totalEntries was 10, and you performed $totalEntries--; then $totalEntries would now be 9.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum