Forum Moderators: coopster

Message Too Old, No Replies

how to customize this?

         

PHPycho

4:52 am on Jun 25, 2007 (gmt 0)

10+ Year Member



Hello forums!
I am not getting the solution for the following case:
My query results gives the following results:
--------------------
¦ field1 ¦ field 2 ¦
---------------------
¦ name1 ¦ value1 ¦
¦ name1 ¦ value2 ¦
¦ name1 ¦ value3 ¦
¦ name2 ¦ value4 ¦
¦ name2 ¦ value5 ¦
:
:
etc.

while($row = ..){
//show results here..
}

I would like to show the results as:
name1
-value1
-value2
-value3

name2
-value4
-value5

without displaying like
name1
-vaue1
name1
-value2
name1
-value3 etc..

Please help me in this case..
Thanks in advance to all of you

Habtom

6:25 am on Jun 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something like the following:

1--------------------
¦ field1 ¦ field 2 ¦
---------------------
¦ name1 ¦ value1 ¦
¦ name1 ¦ value2 ¦
¦ name1 ¦ value3 ¦
¦ name2 ¦ value4 ¦
¦ name2 ¦ value5 ¦
:

<?php
$p_value = "";
while($row = mysql_fetch_array($that_query)){
if ($p_value == $row['field1']) {
echo $row['field2'];
} else {
echo $row['field1'];
$p_value = $row['field1'];
}
}
?>

Hope this help.s

Habtom

vasiapup

1:08 pm on Jun 25, 2007 (gmt 0)

10+ Year Member



No, what habtom suggested won't work.

Try this:


$result_1 = mysql_query($query_1);
while ($row_1 = mysql_fetch_assoc($result_1)) {
$array[$row_1['field1']][$row_1['field2']]=$row_1['field2'];
}
foreach($array as $key=>$val) {
echo "$key<br />";
foreach($val as $val1) {
echo "-$val1<br />";
}
}

should work okay

Habtom

1:35 pm on Jun 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, what habtom suggested won't work.

Already tested, or you think it won't work? Why wouldn't it work?

Habtom

vasiapup

1:54 pm on Jun 25, 2007 (gmt 0)

10+ Year Member



okay, your code works but there is an error.
here's what it displays:
name1
-value2
-value3
name2
-value5

you see value1 and value4 are lost?

well, your code is okay, but just missing one line.
here's your code fixed:


while ($row_1 = mysql_fetch_assoc($result_1)) {
if ($p_value == $row_1['field1']) {
echo "-".$row_1['field2']."<br />";
} else {
echo $row_1['field1']."<br />";
echo "-".$row_1['field2']."<br />";
$p_value = $row_1['field1'];
}
}

But my code works too, and I think it's written in a better programming style :)