Forum Moderators: coopster

Message Too Old, No Replies

While in another while statement

I want to loop inside a loop, with a simple example

         

Spaciane

9:14 pm on Feb 22, 2004 (gmt 0)

10+ Year Member



Dear PHP friend

I'm still a newbies but enjoy very much php/MySQL. Here's the situation.
I want to improve the display of my $result3. I'm sure I need to use another
"while" statement inside this one but I don't want to create a infinite loop.

The $result3 look's like that:
-------------------------------------------------------
¦norisk¦descris ¦nomesur ¦descmes ¦
-------------------------------------------------------
¦A ¦high ¦1 ¦use harness ¦
-------------------------------------------------------
¦A ¦high ¦7 ¦be carefull ¦
-------------------------------------------------------
¦B ¦wet ¦3 ¦use boot ¦
-------------------------------------------------------
¦B ¦wet ¦7 ¦be carefull ¦
--------------------------------------------------------

I'dont want to had another query( thise one involve already 3 tables). Here's the code I wrote:

$result3 = mysql_query($sql2) or die("9err");
while ($array2 = mysql_fetch_array($result3))
{

print ("<tr><td>");
print ($array2['descris']) or die("err17");

print ("</td><td>");
print ($array2['descmes']) or die("err17");
print ("</td></tr>");
}

Of course, I don't want to see twice "high" and twice "wet" in the display but want to see all the measures
for each risk.

Thank you for answering, all this php/MySQL stuff is very exiting. I came from far.

Spaciane

[edited by: jatar_k at 2:28 am (utc) on Feb. 25, 2004]
[edit reason] no sigs thanks [/edit]

Hanu

9:39 pm on Feb 22, 2004 (gmt 0)

10+ Year Member



Use a loop variable and an if-statement to detect whether your descris has changed. There are two things you need to guarantee for this solution to work:

a) The query result must be sorted by descris and

b) descris must never be the empty string.

$result3 = mysql_query($sql2) or die("9err");
$old = "";
while ($array2 = mysql_fetch_array($result3))
{
if( $old!= $array2['descris'] )
{
if( $old ) {
print( "</tr>$ );
}
$old = $array2['descris'];
print ("<tr><td>");
print ($old) or die("err17");
}
print ("</td><td>");
print ($array2['descmes']) or die("err17");
}
if( $old ) {
print( "</td></tr>$ );
}

Spaciane

2:09 am on Feb 23, 2004 (gmt 0)

10+ Year Member



Hi Hanu, thank you for your response

1)Yes condition a) and b) are met.

2)I'm still trying to sort it out. But I don't understand the use of $ in the html stuff like:

{
print( "</td></tr>$ );
}

3)Here is the message I got
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in ...submit_ec_dev.php on line 71

How can I find those line, right now I'm finding them by try and misstake?

Hanu

12:58 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



Sorry, that was a typo. I didn't mean the dollar sign ($) but quotation marks ("):


$result3 = mysql_query($sql2) or die("9err");
$old = "";
while ($array2 = mysql_fetch_array($result3)) {
if ($old!= $array2['descris']) {
if ($old) {
print( "</tr>" );
}
$old = $array2['descris'];
print ("<tr><td>");
print ($old) or die("err17");
}
print ("</td><td>");
print ($array2['descmes']) or die("err17");
}
if ($old) {
print ("</td></tr>");
}

Spaciane

1:59 am on Feb 25, 2004 (gmt 0)

10+ Year Member



I did change the $ for " but somehow it wasn't working so I posted my 2sd messages to understand more your script. I experiment lot's of deifferent ways and still it wasn't working. Now I understand more the "if" condition and the!= .After modifying your script a bit I did get exactly the result I was looking for.You should see all the stuff I tryed before (if, else, including a var $first). A whole lot of fun.

here's the results :

$result3 = mysql_query($sql2) or die("9err");
$old = "";
while ($array2 = mysql_fetch_array($result3)) {
if ($old!= $array2['descris']) {
$old = $array2['descris'];
print ("<tr><td>");
print ($old) or die("err17");
print ("</td><td>");
}
print ($array2['descmes']) or die("err17");
print ("<br>");
}
if ($old) {
print ("</td></tr>");
}

very clean and simple

Thank you very much Hanu

Spaciane

[edited by: jatar_k at 2:28 am (utc) on Feb. 25, 2004]
[edit reason] no sigs thanks [/edit]