Forum Moderators: coopster

Message Too Old, No Replies

Closing table and removing spare row

         

Jamier101

8:05 pm on Sep 13, 2010 (gmt 0)

10+ Year Member



I have a table that my results print into:


if(mysql_num_rows($query) > 0){

//loop through the results
echo "<table border='1'>";
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($query)) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo "";
echo $row['id']."<br>";
echo "<tr><td>";
echo $row['owner']."<br>";
echo "<tr><td>";
echo $row['location']."<br>";
echo "<tr><td>";
echo $row['area']."<br>";
echo "<tr><td>";
echo $row['community']."<br>";
echo "<tr><td>";
echo $row['type']."<br>";
echo "<tr><td>";
echo $row['bedrooms']."<br>";
echo "<tr><td>";
echo "<br>";
echo "There are $row[0] results for your search";
}


At the minute there is always a spare row at the bottom of the table because, this is because the table puts a single cell between results, is there a way of removing the spare cell from the last result? Ideally I would still like there to be a space between the bottom of the table and my $row[0] statement.

enigma1

6:38 pm on Sep 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to close the row before opening a new one and use the table cells for the various elements to construct the whole row. For example:

echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['owner'] . '</td>';
...................
echo '</tr>';

And so you don't need the line breaks the row tags will take care of it. Finally move the:

echo "There are $row[0] results for your search";

outside the while loop and close the table first


}
echo '</table>';
echo '<div>There are $row[0] results for your search</div>';

Jamier101

1:03 pm on Sep 26, 2010 (gmt 0)

10+ Year Member



I nearly posted a foolish mistake but then realised the errors of my ways although I've come across an issue I cannot solve, the final statement in my script is to show the number of results the query return.

However, at present I'm only getting:

There are $row[0] results for your search

If I remove the div tags I get:

There are results for your search

Any idea's folks?


//see if the query returned anything ie > 'more than' 0 rows
if(mysql_num_rows($query) > 0){

//loop through the results
echo "Search conducted on: ";
echo date("l, F d, T h:i" ,time());
echo "<p>";
echo "Here are the results of your search:";

// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($query)){

// Print out the contents of each row into a table
echo '<table>';
echo '<tr>';
echo '<td>' . "Villa ID: " . $row['id'] . '</td>';
echo '</tr>';
echo '<td>' . "Contact: " . $row['contact'] . '</td>';
echo '</tr>';
echo '<td>' . "Location: " . $row['location'] . '</td>';
echo '</tr>';
echo '<td>' . "Area: " . $row['area'] . '</td>';
echo '</tr>';
echo '<td>' . "Community: " . $row['community'] . '</td>';
echo '</tr>';
echo '<td>' . "Property Type: " . $row['type'] . '</td>';
echo '</tr>';
echo '<td>' . "Number of Bedrooms: " . $row['bedrooms'] . '</td>';
echo '</tr>';
echo '<br>';
echo '</table>';
}
//close while loop

echo '<div>There are $row[0] results for your search</div>';

enigma1

4:20 pm on Sep 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo '<div>There are ' . mysql_num_rows($query) . ' results for your search</div>';

Jamier101

9:34 pm on Sep 26, 2010 (gmt 0)

10+ Year Member



That's great thanks, if I wanted to the text to change when more than one item was found could I do something like this:


if(mysql_num_rows($query) > 0){

//loop through the results
echo "Search conducted on: ";
echo date("l, F d, T h:i" ,time());
echo "<p>";
if(mysql_num_rows($query) = 1) {
echo '<div> ' . mysql_num_rows($query) . ' villa was found, here are your results:</div>';
}
else{
echo '<div> ' . mysql_num_rows($query) . ' villa(s) were found, here are your results:</div>';
}
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($query)){

// Print out the contents of each row into a table
echo '<table>';
echo '<tr>';
echo '<td>' . "Villa ID: " . $row['id'] . '</td>';
echo '</tr>';
echo '<td>' . "Contact: " . $row['contact'] . '</td>';
echo '</tr>';
echo '<td>' . "Location: " . $row['location'] . '</td>';
echo '</tr>';
echo '<td>' . "Area: " . $row['area'] . '</td>';
echo '</tr>';
echo '<td>' . "Community: " . $row['community'] . '</td>';
echo '</tr>';
echo '<td>' . "Property Type: " . $row['type'] . '</td>';
echo '</tr>';
echo '<td>' . "Number of Bedrooms: " . $row['bedrooms'] . '</td>';
echo '</tr>';
echo '<br>';
echo '</table>';
}
//close while loop

Jamier101

9:35 pm on Sep 26, 2010 (gmt 0)

10+ Year Member



That's great thanks, if I wanted to the text to change when more than one item was found could I do something like this:


if(mysql_num_rows($query) > 0){

//loop through the results
echo "Search conducted on: ";
echo date("l, F d, T h:i" ,time());
echo "<p>";
if(mysql_num_rows($query) = 1) {
echo '<div> ' . mysql_num_rows($query) . ' villa was found, here are your results:</div>';
}
else{
echo '<div> ' . mysql_num_rows($query) . ' villa(s) were found, here are your results:</div>';
}
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($query)){

// Print out the contents of each row into a table
echo '<table>';
echo '<tr>';
echo '<td>' . "Villa ID: " . $row['id'] . '</td>';
echo '</tr>';
echo '<td>' . "Contact: " . $row['contact'] . '</td>';
echo '</tr>';
echo '<td>' . "Location: " . $row['location'] . '</td>';
echo '</tr>';
echo '<td>' . "Area: " . $row['area'] . '</td>';
echo '</tr>';
echo '<td>' . "Community: " . $row['community'] . '</td>';
echo '</tr>';
echo '<td>' . "Property Type: " . $row['type'] . '</td>';
echo '</tr>';
echo '<td>' . "Number of Bedrooms: " . $row['bedrooms'] . '</td>';
echo '</tr>';
echo '<br>';
echo '</table>';
}
//close while loop

enigma1

8:57 am on Sep 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes that should also work or you can assign the number of rows to a variable and then use that to simplify processing a bit.

$result_rows = mysql_num_rows($query);
if($result_rows){
........

Jamier101

12:29 am on Sep 28, 2010 (gmt 0)

10+ Year Member



Okay chaps, I've confused myself whilst doing this, I think I've got it right but somewhere along the line I've obviously messed up, any ideas?


//see if the query returned anything ie > 'more than' 0 rows
if(mysql_num_rows($query) > 0){

//loop through the results
echo "Search conducted on: ";
echo date("l, F d, T h:i" ,time());
echo "<p>";

$result_rows = mysql_num_rows($query);

if($result_rows = 1){
echo '<div> ' . $result_rows . ' villa was found, here are your results:</div>';
(
else{
echo '<div> ' . $result_rows . ' villa(s) were found, here are your results:</div>';
}

enigma1

9:29 am on Sep 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if($result_rows == 1){

Jamier101

4:48 pm on Sep 28, 2010 (gmt 0)

10+ Year Member



Thanks, it finally works :)


//loop through the results
echo "Search conducted on: ";
echo date("l, F d, T h:i" ,time());
echo "<p>";

if(mysql_num_rows($query) == 1) {
echo '<div> ' . mysql_num_rows($query) . ' villa was found, here are your results:</div>';
}
else{
echo '<div> ' . mysql_num_rows($query) . ' villa(s) were found, here are your results:</div>';
}