Forum Moderators: coopster

Message Too Old, No Replies

Changing row type in a while statement?

while but one <td> one <th>

         

Gruessle

7:50 am on Feb 26, 2005 (gmt 0)

10+ Year Member




while ($qry = mysql_fetch_array($p_edu)) {
print "
<tr>
<td>$qry[cYear]</td>
<td>$qry[Education]</td>
<td>$qry[School]</td>
<td>$qry[Program]</td>
<td>
<form name='form1' method='post' action=$PHP_SELF>
<input name='delid' type='hidden' value=$qry[id]>
<input type='submit' name='delete' value=Delete>
</form>
</td>
</tr>\n";
}

[edited by: ergophobe at 6:03 pm (utc) on Feb. 26, 2005]
[edit reason] made code readable - linebreaks [/edit]

Gruessle

7:44 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



Thank you ergophobe for making my code more readable.

I am still learning. :)

coopster

11:53 pm on Feb 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What is it that you want to accomplish though, Gruessle? You want a <th> on the first <tr>, but <td>'s after that?

Gruessle

4:58 am on Feb 28, 2005 (gmt 0)

10+ Year Member



No I like to have

<tr>
<th>
<td>
<th>
<td>
<th>
<td>
<th>
<td>

MrSpeed

6:43 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That doesn't seem to make sense. Can you post a complete sample of what you want the rendered table to look like?

Gruessle

9:43 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



You are right it didn't make sence. And you are not a mind reader.

Here it is:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>1</td>
</tr>
<tr>
<th>2</th>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<th>4</th>
</tr>
<tr>
<td>5</td>
</tr>
</table>

ironik

9:55 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



if I understand you, you want some code that will change the 'td' to a 'th' in the while loop?


while ($qry = mysql_fetch_array($p_edu)) {
if ($condition == true)
{
$first = '<th>';
$second = '</th>';
} else {
$first = '<td>';
$second = '</td>';
}
echo '<tr>';
echo $first . $qry['row'] . $second;
echo '</tr>';

Where $condition is whatever you want to determine whether the cells being output should be a 'th' and 'td'.

Gruessle

12:23 am on Mar 1, 2005 (gmt 0)

10+ Year Member



I understand all but the $condition thing.

Shouldn't there be a counter which adds to a veriable and it makes it th when the number is even and td when the number is odd or something like that.

I am thinking like

while ($qry = mysql_fetch_array($p_edu)) {
if ($a == even_number) //There must be away to check that?
{
$b = 'th>'
} else {
$b = 'td>'
}
echo '<tr>';
echo '<'$b . $qry['row'] . </'$b;
echo '</tr>';

$a++
};

Gruessle

12:35 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Maybe like this?

$a = 1;
while ($qry = mysql_fetch_array($p_edu)) {
if ($a % 2 == 0)
{
$b = 'th>'
} else {
$b = 'td>'
}
echo '<tr>';
echo '<'$b . $qry['row'] . </'$b;
echo '</tr>';

$a++
};

crxchaos

12:56 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Gruessle,

Using the code you pasted above:


while ($qry = mysql_fetch_array($p_edu))
{
$tx = ($tx == 'td')? ('th') : ('td');
print "
<tr>
<$tx>$qry[cYear]</$tx>
<$tx>$qry[Education]</$tx>
<$tx>$qry[School]</$tx>
<$tx>$qry[Program]</$tx>
<$tx>
<form name='form1' method='post' action=$PHP_SELF>
<input name='delid' type='hidden' value=$qry[id]>
<input type='submit' name='delete' value=Delete>
</form>
</$tx>
</tr>\n
";
}

I just read another thread about code optimisation and couldn't resist the urge to do away with all those "if" statements!

ironik

1:17 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Nice work. I didn't realise you wanted them alternating the entire way down... just when certain condition has been met. Good to see it's all working though.

Gruessle

4:30 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Wow this looks very sophisticated.
All I could figure out is that? and : are called Ternary Operators but I don't realy understand

Can you be so good and explain this line:

$tx = ($tx == 'td')? ('th') : ('td');

ironik

4:40 am on Mar 1, 2005 (gmt 0)

10+ Year Member



value = (condition)? on true : on false ;

If the condition is true, then the left hand side statement will be assigned to the value, if the condition is false, the right hand side statement will be assigned to the value.

It's like a shorthand if, then, else statement.

In this case it checks that the value equals 'td', if it does, then it changes it to 'th'.

[edited by: ironik at 4:42 am (utc) on Mar. 1, 2005]

Gruessle

4:40 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Actually is doesn't work.

Here is my output - the delete button is the only thing I see and that works.

<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<form name='form1' method='post' action=/usr/autobiography.php>
<input name='delid' type='hidden' value=3>
<input type='submit' name='delete' value=Delete>
</form>
</td>
</tr>



<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th>
<form name='form1' method='post' action=/usr/autobiography.php>
<input name='delid' type='hidden' value=4>
<input type='submit' name='delete' value=Delete>
</form>
</th>
</tr>

</table>

ironik

4:46 am on Mar 1, 2005 (gmt 0)

10+ Year Member



I could be a problem with your data source. The fields are appearing empty... I'd check that the query you have is actually returning a result.

Gruessle

4:52 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Ohh sorry sorry sorry it does work I tested it in the wrong form accessing the wrong table.

This is great. Still don't understand

Now I get that it is supposed to do

when $tx do ('th') OR ('td')

How does it know to alternate and which ons turn it is?

I guess thats done here
($tx == 'td')?
but I don't get the logic

ironik

4:57 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Kind of hard to explain, but because it's in a loop, the '()? :' bit asks, does $tx equal 'td'. If it does, then it gives it a different value, so the next time around the condition is going to evaluate to false... then around and around it goes.

Gruessle

7:01 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Now it makes sence thank you.
It's very nifty