SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Mr Jojo's Avatar
    Join Date
    May 2007
    Posts
    322
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fill tables using foreach

    I'm making a script that asks some data to the DB and stores it in $apps variable

    PHP Code:
    $apps=mysql_fetch_array(mysql_query('SELECT id,user_id,ndowns,nome,data,tamanho,download,tipo,info FROM programas WHERE cat_id=1 ORDER BY data DESC LIMIT 0,40')); 

    And there was a very simple loop code that used the foreach function to fill a table, also, detecting if $apps[n] still contains data, if not, stoping the loop.

    PHP Code:
    foreach($apps as $dados)
    {
        
    $posted mysql_fetch_row(mysql_query('SELECT nick FROM utilizadores WHERE id='.$dados['user_id']));
        echo 
    '        <td align="center" background="tabela2.jpg"><font color="#333333">'.$posted[0].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$dados['ndowns'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$dados['nome'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$dados['tamanho'].'Kb</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333"><a href="download.php?id='
    .$dados['id'].'>Aqui!</a></font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$dados['data'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$dados['tipo'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333"><a href="'
    .$dados['info'].'">Info</a></font></td>';


    But this doesnt work this way :P

    I dont remember half of this script, so i cant complete it, can you help me?
    Last edited by Mr Jojo; Jul 25, 2007 at 13:10.

  2. #2
    SitePoint Addict Mr Jojo's Avatar
    Join Date
    May 2007
    Posts
    322
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "A child's life is like a piece of paper on which every person leaves a mark."
    Chinese Proverb

  3. #3
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I just cant understand the logic behind the way you do it.

    When looping over a query result, it is easier to use while. And you should never run a query inside a loop if you can avoid it.

    I have added a left join in your query and put the loop in a while. It should work, if it dont please look over to see if I have miswritten any of your colum names.

    PHP Code:
    $result mysql_query('SELECT prog.id, prog.user_id, prog.ndowns, prog.nome, prog.data, prog.tamanho, prog.download, prog.tipo, prog.info, utiliz.nick FROM programas as prog LEFT JOIN utilizadores as utiliz ON prog.user_id=utiliz.id WHERE prog.cat_id=1 ORDER BY prog.data DESC LIMIT 0,40') or die(mysql_error()); //Remove the die before making the script live, just use it now when testing the query the first time.

    while ($buffer mysql_fetch_array($result))
        {
        echo 
    '<td align="center" background="tabela2.jpg"><font color="#333333">'.$buffer['nick'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$buffer['ndowns'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$buffer['nome'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$buffer['tamanho'].'Kb</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333"><a href="download.php?id='
    .$buffer['id'].'>Aqui!</a></font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$buffer['data'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333">'
    .$buffer['tipo'].'</font></td>
            <td align="center" background="tabela2.jpg"><font color="#333333"><a href="'
    .$buffer['info'].'">Info</a></font></td>';
        } 

  4. #4
    SitePoint Addict Mr Jojo's Avatar
    Join Date
    May 2007
    Posts
    322
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW! Amazing!

    Whait, hmmmm, why the table is called programas and you use only prog when you call it and it works fine?

    Omg, so much i have to learn yet :S

  5. #5
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mr Jojo View Post
    Whait, hmmmm, why the table is called programas and you use only prog when you call it and it works fine?
    I am not sure exactly what you are asking.

    Are you wondering about the: "programas as prog" part?

    When you do a join, it is best to seperate the colums you call and limit it against the correct tables, as sometimes two tables can contain two colums with the same name but different values.

    You could do this by adding the table name infront of each colum, but this is cumbersome if the table name is long, so the easiest is to add " as name" after the table name, and then the query tells the database engine that it should refer to that table with the "shorter name" instead.

    If you are not famillar with Joins I would suggest that you do a search on Google for a few good tutorials or ask if someone know of any in the database forum on SitePoint. Using Joins makes you database queryes much more efficient, and your script will become better as well.

  6. #6
    SitePoint Addict Mr Jojo's Avatar
    Join Date
    May 2007
    Posts
    322
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice!
    Now i understand!

    Thanks!


Bookmarks

Posting Permissions

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