Jump to content

Recommended Posts

I have a page that queries a mysql database.  With the data that is pulled (image URLs and the web sites they're associated with), a table that is 2 columns wide.  The number of rows in the table is calculated by counting the rows of data and dividing by 2.  It works well -- with a small logic problem that I can't figure out...

 

If the data is odd-numbered (7 items, 11 items, etc.), the last cell has a missing image (echo "<a href='".$url[$i]."'><img src='".$imageUrl[$i]."' border='0'></a>";).  If the data is even-numbered (8 items, 20 items, etc.), there is an extra -- and empty -- row.

 

What am I missing??

 

$row = mysql_fetch_array($result) or die(mysql_error());
// mysql_fetch_array() returns both an assocative array and a numerically indexed array --
// a combination of the mysql_fetch_row() and the mysql_fetch_assoc() functions

// set some basic variables;
$tr=0;
$flag= 0;
$i=0;

// run through the all the arrays to break them into individual items that I can reference later
while($row = mysql_fetch_object( $result )) {
$item[$flag]= $row->item;
$title[$flag]= $row->title;
$url[$flag]= $row->url;
$imageUrl[$flag]= $row->imageUrl;
$alt[$flag]= $row->alt;
$sex[$flag]= $row->sex;
$shirtType[$flag]= $row->shirtType;
$flag++;
}

// find out how many rows we need
$totalRows=($flag/2);

// begin the table
echo "<table border='2' cellpadding='5' align='center'>";
while ($tr <= $totalRows) {  // create the rows
		$td=1;  // each time through, re-start the column counter
		echo "<tr>";
		while ($td <= 2) {  //create the columns
			echo "<td>";
			echo "<a href='".$url[$i]."'><img src='".$imageUrl[$i]."' border='0'></a>";
		 	$i++;
			echo "</td>\n";
			$td++;
		}
		echo "</tr>";
		$tr++;
	}
echo "</table>";

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/161836-solved-dynamically-building-a-table/
Share on other sites

Thanks!  But, I don't get this comment:

 

"To modify this for an array, just replace the while loop with a for loop and use your array as the input instead of the mysql fetch results."

 

What would I do on the script I attached earlier?  What would I change?

<?php

/* START TABLE */

echo '
<table border="2" cellpadding="5" align="center">
   <tr>
';

$num = mysql_num_rows( $result );
$i = 0;
while ( $row = mysql_fetch_object ( $result ) )
{

$i++;

if ( ($num % 2 != 0 ) && ( $i == $num ) )
{
	echo '   <tr>
';
	echo '      <td colspan="2">
';
	echo '         <a href="' . $row->url . '" ><img src="' . $row->imageUrl . '" border="0" ></a>
';
	echo '      </td>
';
	echo '   </tr>
';
}
else
{
	echo '      <td>
';
	echo '         <a href="' . $row->url . '" ><img src="' . $row->imageUrl . '" border="0" ></a>
';
	echo '      </td>
';

	if ( $i % 2 == 0 )
	{

		echo '   </tr>
';
		if ( $i != $num )
		{
			echo '   <tr>
';
		}
	}
}
}

echo '</table>';

/* TABLE DONE */

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.