Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.

    Code:
    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:
    Code:
    ----------------------------------------------------------------------
    | 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!

  • #2
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by shadowsai View Post
    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.

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts
    I dont know the fieldnames but if it were then .... e.g.

    PHP Code:
    $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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages View Post
    I dont know the fieldnames but if it were then .... e.g.

    PHP Code:
    $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?

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts
    Quote Originally Posted by shadowsai View Post
    ...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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    to do something on row 0 you can use this:

    PHP Code:

    $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";
        } 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •