Adding column at end of loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Adding column at end of loop

Post by reecec »

Hi i posted about this yesterday but thoguht i found the reason but there is something else to it so i have posted an new topic

This is my code:

Code: Select all

while ($field=mysql_fetch_field($result)) {

echo "<th bgcolor='#F3F3F3'>";
echo "$field->name";
echo "</th>";
}

echo "<th bgcolor='#F3F3F3'>";
echo "test";
echo "</th>";




$result2=mysql_query("SELECT * FROM $url");
$count=mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
$colour=1;
for ($i=0; $i<mysql_num_fields($result); $i++) {
echo $i;
if ($colour==1) 
{
echo "<td bgcolor='#E5E5E5'>";
echo "$row[$i]";
echo "</td>";
}
$i++;
$colour=2;
if ($colour==2) 
{
echo "<td bgcolor='#C1C1C1'>";
echo "$row[$i]";
echo "</td>";
}

echo $i;
$fields=mysql_num_fields($result);
$newfields=$fields-1;
echo "<strong>$newfields</strong>";
if ($i==$newfields) 
{
echo "<td bgcolor='#C1C1C1'>";
?>

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">





<?
echo "</td>";

   echo '<td bgcolor="#C1C1C1">';
  ?>

<A href="editrow.php?id=<? echo $row['0']; ?>&table=<? echo $url; ?>" />Edit</td>


<?

}




$colour=1;
}
}
What my code does is when the loop count is the same as one less than num rows it adds a column it works fine most of the time but on some tables it does add it
This is where it hasnt worked
on the top to show how it works i have echoed the while count in non bold then the num rows -1 is bold (on this one where it hasnt worked the Bold is lower then the maximum non bold)

Image


This one is fine: the bold is the same as the maximum non bold so it has added the column



Image

i dont know if i have explaied this well but you will see from the code and the images.

thanks reece
Last edited by reecec on Wed Oct 25, 2006 9:00 am, edited 1 time in total.
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Incrementing "$i" to much.

Post by churt »

Using $i++ in the middle of the loop causes the loop to increment twice each time through. Therefore an odd number of fields will never be equal to "$fields - 1".

Try useing the following modified code:

Code: Select all

while ($field=mysql_fetch_field($result)) {

echo "<th bgcolor='#F3F3F3'>";
echo "$field->name";
echo "</th>";
}

echo "<th bgcolor='#F3F3F3'>";
echo "test";
echo "</th>";




$result2=mysql_query("SELECT * FROM $url");
$count=mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
$colour=1;
for ($i=0; $i<mysql_num_fields($result); $i++) {
echo $i;

//Start modified code here.
if ($colour==1)
{
echo "<td bgcolor='#E5E5E5'>";
$colour=2;
}

if ($colour==2)
{
echo "<td bgcolor='#C1C1C1'>";
$colour=1;
}

echo "$row[$i]";
echo "</td>";
//End modifed code here

echo $i;
$fields=mysql_num_fields($result);
$newfields=$fields-1;
echo "<strong>$newfields</strong>";
if ($i==$newfields)
{
echo "<td bgcolor='#C1C1C1'>";
?>

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">





<?
echo "</td>";

   echo '<td bgcolor="#C1C1C1">';
  ?>

<A href="editrow.php?id=<? echo $row['0']; ?>&table=<? echo $url; ?>" />Edit</td>


<?

}




$colour=1;
}
}
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

hi thanks for your reply

i have added your new code it has added the new rows is all out of place


New Code:
Image


Before New:

Image

thanks reece
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?
	
	while ($field = mysql_fetch_field($result)) {
		
		echo '<td bgcolor="#F3F3F3">'. $field->name.'</td>';
	}
	
	/**
	 * I don't know why you have this query here, nothing to do
	 * with the code here
	*/ 
	$result2= mysql_query("SELECT * FROM $url") or die(mysql_error());
	
	$count = mysql_num_fields($result);
	while ($row = mysql_fetch_assoc($result)) {
		echo '<tr>';
		foreach ($i=0; $i <= $count; $i++) {
			echo '<td bgcolor="#'. ($i % 2 == 1 ? 'E5E5E5' : 'C1C1C1').'"'. $row[$i].'</td>';
		}
?>
		<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['id']; ?>">
<?
		echo "</td>";
		echo '<td bgcolor="#C1C1C1">';
?>
		<a href="editrow.php?id=<? echo $row['id']; ?>&table=<? echo $url; ?>" />Edit</a>
<?
	}
?>		</td>
This is a simplified version of yours. Not to mention you had a missing closing tag. Just make sure your actual id field name is called id.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

The code gives an error of unxepected ;

here

Code: Select all

foreach ($i=0; $i <= $count; $i++) {

but isnt this how it goes
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

foreach :arrow: for
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thankd feyd that sorted the error

the code from Jcart's code has totaly changed the table
Image

i we need to go back to the old unless anyone can see why

thanks reece
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The checkboxes are being output after the table cells are closed, thus making them move outside of the table itself in rendering.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

Thanks yes but the whole codes changed as all table data has not been entered.

My first post shows where it was fine
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Sorry missed something

Post by churt »

Sorry, my origian post was doing both colors each time.

Therefore it was getting two <td> tags each time which caused the offset.

This should work better.

Code: Select all

while ($field=mysql_fetch_field($result)) {

echo "<th bgcolor='#F3F3F3'>";
echo "$field->name";
echo "</th>";
}

echo "<th bgcolor='#F3F3F3'>";
echo "test";
echo "</th>";




$result2=mysql_query("SELECT * FROM $url");
$count=mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
$colour=1;
for ($i=0; $i<mysql_num_fields($result); $i++) {
echo $i;

//Start modified code here.
if ($colour=='#E5E5E5'){ $colour='#C1C1C1'; }else{ $colour='#E5E5E5'; }
echo "<td bgcolor='$colour'>".$row[$i]."</td>";
//End modifed code here

echo $i;
$fields=mysql_num_fields($result);
$newfields=$fields-1;
echo "<strong>$newfields</strong>";
if ($i==$newfields)
{
echo "<td bgcolor='#C1C1C1'>";
?>

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">


<?
echo "</td>";

   echo '<td bgcolor="#C1C1C1">';
  ?>

<A href="editrow.php?id=<? echo $row['0']; ?>&table=<? echo $url; ?>" />Edit</td>


<?

}




$colour=1;
}
}
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks thats worked perfect and even the table that dint work before work now will this now not only work for sum but for all ??

sorry just another thing how do i alternate the colour row instead of the column i think it will have to be when $i count = the last field change the colour am i right


So something like this but you can put an if in an if:

Code: Select all

$count=mysql_num_fields($result);

$colourcount=$count-1;


if ($i=$colourcount)
{

if ($colour=='#E5E5E5'){ $colour='#C1C1C1'; }else{ $colour='#E5E5E5'; }

}
thanks reece
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Post by churt »

Are you wanting to toggle between pairs of background colors for the cells when the row changes?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

yes thats what i hope lol
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Will this do it.

Post by churt »

Try this.

Code: Select all

while ($field=mysql_fetch_field($result)) {

echo "<th bgcolor='#F3F3F3'>";
echo "$field->name";
echo "</th>";
}

echo "<th bgcolor='#F3F3F3'>";
echo "test";
echo "</th>";




$result2=mysql_query("SELECT * FROM $url");
$count=mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {

//Add this line*****************************************
if ($clr1=='#E5E5E5'){ $clr1='blue'; $clr2='green'; }else{ $clr1='#E5E5E5'; $clr2='#C1C1C1'; }
//**************************************************/
echo '<tr>';
$colour=1;
for ($i=0; $i<mysql_num_fields($result); $i++) {
echo $i;

//Start modified code here.
if ($colour==$clr1){ $colour=$clr2; }else{ $colour=$clr1; }//Change this one.
echo "<td bgcolor='$colour'>".$row[$i]."</td>";
//End modifed code here

echo $i;
$fields=mysql_num_fields($result);
$newfields=$fields-1;
echo "<strong>$newfields</strong>";
if ($i==$newfields)
{
echo "<td bgcolor='#C1C1C1'>";
?>

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">


<?
echo "</td>";

   echo '<td bgcolor="#C1C1C1">';
  ?>

<A href="editrow.php?id=<? echo $row['0']; ?>&table=<? echo $url; ?>" />Edit</td>


<?

}




$colour=1;
}
}
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks for your help

umm all my rows are black sorry
Post Reply