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 7 of 7

Thread: looping

  1. #1
    Regular Coder slappyjaw's Avatar
    Join Date
    Mar 2009
    Location
    Wonderland!
    Posts
    146
    Thanks
    14
    Thanked 3 Times in 3 Posts

    Unhappy looping

    Hello is there a way to make it so that you can loop through two different record sets in php. this is my code
    PHP Code:
    <?php require_once('Connections/mysql.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function 
    GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      
    $theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

      
    $theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch (
    $theType) {
        case 
    "text":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;    
        case 
    "long":
        case 
    "int":
          
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case 
    "double":
          
    $theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
          break;
        case 
    "date":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;
        case 
    "defined":
          
    $theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
          break;
      }
      return 
    $theValue;
    }
    }
    session_start();
    $colname_friends "-1";
    if (isset(
    $_SESSION['kt_login_user'])) {
      
    $colname_friends $_SESSION['kt_login_user'];
    }
    mysql_select_db($database_mysql$mysql);
    $query_friends sprintf("SELECT * FROM friends WHERE username = %s"GetSQLValueString($colname_friends"text"));
    $friends mysql_query($query_friends$mysql) or die(mysql_error());
    $row_friends mysql_fetch_assoc($friends);
    $totalRows_friends mysql_num_rows($friends);

    $friendid $row_friends['friendid'];
    mysql_select_db($database_mysql$mysql);
    $query_userpics "SELECT id, username, defaultpic FROM users WHERE id=$friendid";
    $userpics mysql_query($query_userpics$mysql) or die(mysql_error());
    $row_userpics mysql_fetch_assoc($userpics);
    $totalRows_userpics mysql_num_rows($userpics);

    $title "Your friends";
    //INCLUDE THE HEADER!
    include("user_header.php");
    ?>
    </div>
    <head>
    </head>

    <body>
    <table width="200" border="1">
            <tr>
              <td height="134"><img src="user_uploads/<?php echo $row_userpics['id']; ?>/<?php echo $row_userpics['defaultpic']; ?>" width="201" height="161" alt=""></td>
            </tr>
            <tr>
              <td><div align="center"><?php echo $row_userpics['username']; ?></div></td>
            </tr>
    </table>
    </body>
    </html><?php

    mysql_free_result
    ($friends);

    mysql_free_result($userpics);
    ?>
    its a script that displays a users friends based off of their username which i gathered from one table but then it gathers the username/default picture to display from another table based off the userid that was specified from table one.Thanks for any help! I have tried a loop but it will only loop the username but not the picture? that stays the same for some reasion.
    Last edited by slappyjaw; 08-29-2009 at 08:47 PM.

  • #2
    Regular Coder slappyjaw's Avatar
    Join Date
    Mar 2009
    Location
    Wonderland!
    Posts
    146
    Thanks
    14
    Thanked 3 Times in 3 Posts
    actually i think im going to try to use the join operation? idk if that will do the trick

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    52
    Thanks
    6
    Thanked 1 Time in 1 Post
    Neat! I don't understand most of the code because I am still a newb but cool. Also this is in the wrong section should be in the php scripts section...

  • #4
    Regular Coder slappyjaw's Avatar
    Join Date
    Mar 2009
    Location
    Wonderland!
    Posts
    146
    Thanks
    14
    Thanked 3 Times in 3 Posts
    yea the looping isnt working. i jsust need it to displaye teh users friends throught a loop is there a way to do that with two different tables present.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by firetag View Post
    Neat! I don't understand most of the code because I am still a newb but cool. Also this is in the wrong section should be in the php scripts section...
    No it doesn't, this is a question not a snippet.
    Join them, there is no reason to loop over the results of one and re-query the db.
    PHP Code:
    $sQuery sprintf('
        SELECT
            u.id, u.username, u.defaultpic FROM users u
        INNER JOIN friend f ON (u.id = f.friendid)
        WHERE id=%s'
    GetSQLValueString($colname_friends"text")); 
    That should only return one resultset though, since $colname_friends is scalar. If it were an array, you can change you're where to an IN and implode the $colname_fiends.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    slappyjaw (08-29-2009)

  • #6
    Regular Coder slappyjaw's Avatar
    Join Date
    Mar 2009
    Location
    Wonderland!
    Posts
    146
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Hey wow thank yo uvery much for the reply very helpful. in an attempt to make it easier to work with i have redone the code with your addition.
    PHP Code:
    <?php require_once('Connections/mysql.php'); ?>
    <?php

    session_start
    ();
    $uun $_SESSION['kt_login_user'];

    mysql_select_db('rubygir_slappyjaw');
    $sql "SELECT u.id, u.username, u.defaultpic FROM users u INNER JOIN friends f ON (u.id = f.friendid) WHERE f.username = '$uun'";
    $result mysql_query($sql) or die(mysql_error()); 
    $row mysql_fetch_assoc($result);
    $num mysql_num_rows($result);

    $title "Your friends";
    //INCLUDE THE HEADER!
    include("user_header.php");
    ?>

    </div>
    <head>
    </head>

    <body>
    </body>
    </html>
    would this be ok to use it goes with no errors but when i try to echo information it shows nothing.Thank you very much.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This will only return results where records match in both the users and friends table using the users id and friends friendid as the corresponding tables. It will be empty unless records exist in both. Unfortunately, I'm not quite certain what you're looking for or how you're structure exists to make a determination on if you need to alter you're join to a LEFT or RIGHT join.

    Edit:
    What am I thinking, I know exactly what you're doing here. You're getting the id, username and defaultpic of any user that is a friend to $uun correct?
    That should work.
    Last edited by Fou-Lu; 08-30-2009 at 05:23 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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