#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Error in LOOP on a while


    what I'm trying to do is the following: to relate two tables that give me the ID of certain users, after that I search the messages of these users (table feeds) and finally do a search (in the users table) to get the pictures of each User to be displayed along with the message ...

    All goes well until researching the photos (in the users table), if hi delete this part of the script works 100%, but with this search i only get 1 result ...

    I know this probably is not the best way to work but I am starting on php and I do not know ...


    $rsrel = mysql_query("SELECT * FROM relpede WHERE user_id='$Id_user' UNION SELECT * FROM relaceita WHERE user_id='$Id_user'") or die ("Opps!! Tabela");
    $numamigos = mysql_num_rows($rsrel);

    if($numamigos <= 0){ $alertamigos= "error msg";}
    else{
    while ($rowrel = mysql_fetch_array($rsrel)){
    $amigo_id = $rowrel["user_idamigo"];

    $rs_feed = mysql_query("select * from feeds WHERE id_user = '$amigo_id' ORDER BY data_in DESC") or die ("Opps!! Tabela");
    while ($row_feed = mysql_fetch_array($rs_feed)){
    $id_sender = $row_feed["id_user"];
    $sender = $row_feed["sender"];
    $data_mysql = $row_feed["data_in"];
    $timestamp = strtotime($data_mysql);
    $datetime = date('d/m h:i', $timestamp); // Resultado: 09/02 ~ 20:03
    $text = $row_feed["postit"];
    $excerto=substr($text,0,500);
    $get_classe = $row_feed["classe"];
    if($get_classe =='urgente'){$css_border_post='news_home2';} else {$css_border_post='news_home';}

    //echo $id_sender; only for testing if i get the user from the first 2 tables and all its OK



    The problem starts here he only give 1 result... donīt make the loop for each $id_sender...


    //GIVE PHOTO OF EACH USER
    $rssender = mysql_query("SELECT * FROM usuario WHERE Id='$id_sender'") or die ("Opps!! Tabela membros");
    while ($rowsender = mysql_fetch_array($rssender)){
    $sender_foto = $rowsender["foto"];


    <div id="<? echo $css_border_post; ?>">
    <img class="foto_sender_post" src="images/users/<? echo $sender_foto; ?>" width="50" height="50" />
    <span class="txt_post"><? echo nl2br($text); ?></span>


    <div class="link_opiniao" id="link_opiniao">
    <span class="link_opiniao"><? echo $datetime; ?></span><br />
    <a class="link_opiniao" href="#">Gosto</a>.
    <a class="link_opiniao" href="#">Comentar</a>.
    <a class="link_opiniao" href="#">Partilhar</a>

    </div>
    </div>
    <? }}}}?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,542
    Rep Power
    595
    1) If you are starting on PHP DO NOT use the depreciated MySQL library. Use PDO and get on board with OOP.

    2) Please wrap you code with [ PHP ] tags. See ManiacDan's New User Guide.

    3) If you are getting only 1 row then it means there is only 1 row that meets your criteria. It is poor programming practice to use literals in your queries. You should build a string (like $query="SELECT ...") then echo that string to make sure it is what you intended. You can copy that string and paste it into a mysql command line to see what it returns and adjust the query string accordingly. Much easier to debug that way.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    hi. thanks for your reply and for your tips.

IMN logo majestic logo threadwatch logo seochat tools logo