...

View Full Version : Something different for the first item in each row of a foreach loop through an array



shadowsai
11-11-2012, 06:52 AM
No idea how to do this. Help guys?

So I have this neat little loop that basically goes through my SQL search results (through an index of songs), and tabulates the results.


while($row = mysql_fetch_row($result))
{
echo "<tr>";

// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";

echo "</tr>\n";
}

What I want to do is for the first item in $row, link to that song using the $id. $id has been defined as $id = mysql_query("SELECT ID FROM $table ORDER BY `Title` ASC");

Ideally, the output will be something like:


----------------------------------------------------------------------
| Title | Author | Lyrics |
----------------------------------------------------------------------
|<a href="$ID">Title</a> | anon. | foo |
----------------------------------------------------------------------


How can I modify my foreach loop such that only on $row[0], it does something else?

Thank you!

minder
11-11-2012, 07:06 AM
No idea how to do this. Help guys?


How can I modify my foreach loop such that only on $row[0], it does something else?



You could put a counter in the loop starting at 0 for each row and increments on each pass through the loop. On each pass, check if the counter value == 0 and if it does add the code for the link to the value of the cell.

firepages
11-11-2012, 11:06 AM
I dont know the fieldnames but if it were then .... e.g.



$q=mysql_query("SELECT `ID`,`Title`,`Lyric` FROM `$table` ORDER BY `Title` ASC");
while($r=mysql_fetch_assoc($q)){
echo '<tr>';
echo "<td>{$r['ID']}</td><td>{$r['Title']}</td><td>{$r['Lyric']}</td>";
} echo '</tr>';


should be the sort of thing you are looking for

shadowsai
11-12-2012, 07:54 AM
I dont know the fieldnames but if it were then .... e.g.



$q=mysql_query("SELECT `ID`,`Title`,`Lyric` FROM `$table` ORDER BY `Title` ASC");
while($r=mysql_fetch_assoc($q)){
echo '<tr>';
echo "<td>{$r['ID']}</td><td>{$r['Title']}</td><td>{$r['Lyric']}</td>";
} echo '</tr>';


should be the sort of thing you are looking for

Yeah, shortly after I posted this thread, I realized that I would have to use the while loop. My code was almost exactly like that, except I had each <td></td> in it's own echo. Is that a bad thing, or should I try to combine things into echo statements whenever possible?

firepages
11-12-2012, 08:10 AM
...Is that a bad thing, or should I try to combine things into echo statements whenever possible?

I think its personal preference there are probably pros and cons either way, use whatever you feel comfortable with, there is possibly a performance difference one way or the other but I suspect too small to worry about.

Arcticwarrio
11-12-2012, 09:32 AM
to do something on row 0 you can use this:




$row = mysql_fetch_row($result)
echo "<tr>";
foreach($row as $cell)
echo "<th>$cell</th>";
echo "</tr>\n";

while($row = mysql_fetch_row($result))
{
echo "<tr>";

// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";

echo "</tr>\n";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum