...

View Full Version : looping



slappyjaw
08-29-2009, 06:32 PM
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 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.:eek:Thanks for any help!:thumbsup: I have tried a loop but it will only loop the username but not the picture? that stays the same for some reasion.

slappyjaw
08-29-2009, 07:16 PM
actually i think im going to try to use the join operation? idk if that will do the trick

firetag
08-29-2009, 07:26 PM
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...

slappyjaw
08-29-2009, 07:51 PM
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.

Fou-Lu
08-29-2009, 10:33 PM
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.


$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.

slappyjaw
08-29-2009, 11:42 PM
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 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.

Fou-Lu
08-30-2009, 05:15 PM
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.


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum