Log in

View Full Version : Display Results in rows



e1seix
07-21-2007, 08:15 PM
I need a code to display a search result from mysql.

If I'm using a table for example, display the first 4 results in the first row - <tr> tag, the next 4 on the next row and the next 4 on the next one etc.

I also need to set a limit as to how many results can be displayed, say 16 - so 4 rows, before I have a "next page" link to continue displaying the remaining results in a similar fashion.

can someone help? i'm hopeless at if statements which is what i feel this requires. lol :)

e1seix
07-22-2007, 01:41 PM
no-one... please...

Twey
07-22-2007, 02:09 PM
<table>
<?php
$rowwidth = 4;

for($i = @$_GET['start'] or 0; $row = mysql_fetch_array($rs) && $i < @$_GET['num'] or 16; ++$i) {
if($i &#37; $rowwidth === 0) {
if($i !== @$_GET['start'] or 0) {
?>
</tr>
<?php } ?>
<tr>
<?php } ?>
<td><?php echo $row['result']; ?></td>
<?php } ?>
</tr>
</table>

e1seix
07-22-2007, 03:38 PM
that certainly explains it, but i can't seem to alter it to fit in with the following code:

<td>

$ID = $_GET['ID'];

$result = mysql_query("SELECT * FROM fragrances WHERE ID=$ID ORDER BY PRO")
or die(mysql_error());

echo "<table border='0' cellpadding='5' cellspacing='0' width='500'>";
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td align='center' bgcolor='#ffffff' width='125'><p class='brand_div'>";
echo $row['ima_link'];
echo "</p></td></tr><tr><td align='center' bgcolor='#ffffff' width='125'><p class='brand_head'>";
echo $row['PRO'];
echo "</p></td></tr><tr><td align='center' bgcolor='#ffffff' width='125'><p class='brand_cli'>";
echo $row['cli_link'];
echo "</p></td></tr><tr><td align='center' bgcolor='#ffffff' width='500'><hr color='#cccccc' width='125'></td></tr>";
}

echo "</table>";
?>

</td>

can someone help me. i know it's more complex but i really do appreciate all this help, you're lifesavers - honestly!

e1seix
07-22-2007, 08:29 PM
ooh, i really hope someone can. i'm very confused and can't apply the code to my template without getting error messages...

Twey
07-22-2007, 10:34 PM
<table>
<?php
$rowwidth = 4;
if(!is_numeric($_GET['id']))
die('Invalid ID specified.');
$rs = mysql_query('SELECT * FROM fragrances WHERE ID=' . $_GET['id'] . ' ORDER BY PRO');

for($i = @$_GET['start'] or 0; $row = mysql_fetch_array($rs) && $i < @$_GET['num'] or 16; ++$i) {
if($i &#37; $rowwidth === 0) {
if($i !== @$_GET['start'] or 0) {
?>
</tr>
<?php } ?>
<tr>
<?php } ?>
<td><?php echo $row['result']; ?></td>
<?php } ?>
</tr>
</table>A lot of your markup is deprecated. Try to validate (http://validator-test.w3.org/) with HTML 4.01 Strict.

mwinter
07-22-2007, 11:03 PM
for($i = @$_GET['start'] or 0; $row = mysql_fetch_array($rs) && $i < @$_GET['num'] or 16; ++$i) {
if($i % $rowwidth === 0) {
if($i !== @$_GET['start'] or 0) {


Rather than these ors, which will need to be re-evaluated on subsequent iterations, why not just use separate variables?



$start = isset($_GET['start']) ? $_GET['start'] : 0;
$count = isset($_GET['num']) ? $_GET['num'] : 16;
$i = $start;
while (($row = mysql_fetch_array($rs)) && ($i < $count)) {
if (($i % $rowwidth) == 0) {
if ($i != $start) {
/* ... */
}
/* ... */
}
++i;
}




that certainly explains it, but i can't seem to alter it to fit in with the following code:

It would seem that you've missed the opening <?php delimiter. However, I would also make some further changes. The most important I will address specifically:





$ID = $_GET['ID'];

$result = mysql_query("SELECT * FROM fragrances WHERE ID=$ID ORDER BY PRO")
or die(mysql_error());


Never, ever include externally obtained data in a database query without validating and sanitising it - the latter may not always be necessary. This opens oneself to SQL injection attacks. Typically, database error messages shouldn't be displayed either for the same reason; it also looks very unprofessional.

If $ID is meant to be a number, either coerce it to one or use pattern matching. Either way, errors need to be handled gracefully.



<td>
<?php
// The ID parameter should be tested for existence using the isset function, with
// either a default value or an error-handling mechanism in place if missing.
//
// Assuming this is just an integer, sanitation is not necessary as a number will
// never be harmful (though it might be an invalid or unexpected value).
$ID = (int) $_GET['ID'];


$result = mysql_query("SELECT * FROM fragrances WHERE ID=$ID ORDER BY PRO");
if (!$result) {
// Redirect to an error page that does something nice, such as apologise for
// the problem - don't get technical.
// One could even take the opportunity to send an e-mail to notify someone
// of a problem, though obviously this would need to be logged to prevent it
// from being sent many times.
}
?>
<table border="0" cellpadding="5" cellspacing="0" width="500">
<?php
while (($row = mysql_fetch_array($result))) {
?>
<tr>
<td align="center" bgcolor="#ffffff" width="125">
<p class="brand_div"><?php echo $row['ima_link']; ?></p>
</td>
</tr>
<tr>
<td align="center" bgcolor="#ffffff" width="125">
<p class="brand_head"><?php echo $row['PRO']; ?></p>
</td>
</tr>
<tr>
<td align="center" bgcolor="#ffffff" width="125">
<p class="brand_cli"><?php echo $row['cli_link']; ?></p>
</td>
</tr>
<tr>
<td align="center" bgcolor="#ffffff" width="500">
<hr color="#cccccc" width="125">
</td>
</tr>
<?php
}
?>
</table>
</td>

If your data is really tabular, use CSS instead of presentational attributes. Using tbody elements may be useful in grouping each block, perhaps making the hr element obsolete. If the data isn't tabular, drop the tables: table-based layouts are an abomination.

Hope that sets you on the right track.

e1seix
07-23-2007, 12:34 AM
dudes, i really appreciate all you've done for me... but i'm just not getting it. call me stupid or a novice...

this might give you some idea as to what i'm looking to achieve. the two lots of code you have given me only return error messages. hundreds of repetitive ones!

http://www.dogfightuk.com/byBrand.php?ID=4511

the idea is to display all the "products" four in a row in a cascading fashion

bear in mind i am still only at the very initial construction stage so of course i will have a redirection page and i am aware of the opening <?php. i just pasted the basic php code to see if you could make any sense.

i'm beginning to wonder if this is achievable, by me at least.

try and talk me through this... please!