Page 1 of 1

Adding column at end of loop

Posted: Tue Oct 24, 2006 10:46 am
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

Incrementing "$i" to much.

Posted: Tue Oct 24, 2006 1:58 pm
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;
}
}

Posted: Tue Oct 24, 2006 3:31 pm
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

Posted: Tue Oct 24, 2006 3:59 pm
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.

Posted: Tue Oct 24, 2006 4:17 pm
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

Posted: Tue Oct 24, 2006 5:25 pm
by feyd
foreach :arrow: for

Posted: Tue Oct 24, 2006 5:55 pm
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

Posted: Tue Oct 24, 2006 6:12 pm
by feyd
The checkboxes are being output after the table cells are closed, thus making them move outside of the table itself in rendering.

Posted: Tue Oct 24, 2006 6:15 pm
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

Sorry missed something

Posted: Wed Oct 25, 2006 8:00 am
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;
}
}

Posted: Wed Oct 25, 2006 8:56 am
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

Posted: Wed Oct 25, 2006 11:37 am
by churt
Are you wanting to toggle between pairs of background colors for the cells when the row changes?

Posted: Wed Oct 25, 2006 11:39 am
by reecec
yes thats what i hope lol

Will this do it.

Posted: Wed Oct 25, 2006 2:06 pm
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;
}
}

Posted: Wed Oct 25, 2006 3:01 pm
by reecec
thanks for your help

umm all my rows are black sorry