...

View Full Version : Resolved stuck on a while loop



nclemale
09-06-2011, 11:55 PM
hi . am editting a code i have on a gallery script. so its has a download function for users and also a hits counter.
my hits counter and downloading code works perfect but am struggling with the while loop and how to get it so the link shows under each picture .
At present its showing the first picture then the download links for all 5 pictures that is displaying.
quick example.
[] <<< pic 1
link for pic 1
link for pic 2
link for pic 3
link for pic 4
link for pic 5
[] <<< pic 2
[] <<< pic 3
[] <<< pic 4
[] <<< pic 5
Thats what the page looks like . I do like to try to give you guys as much info as possible lol



if($action=="males")
{
addonline(getuid_sid($sid),"Male Gallery","");
echo "<head>";
echo "<title>Gallery Pics</title>";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../themes/$theme[0]\">";
echo "</head>";
echo "<body>";

echo "<p align=\"center\"><small>";
echo "<i><b><u>Male Users From $sitename</u></b></i>";
echo "<br/><br/>";
echo "</small></p>";
//////ALL LISTS SCRIPT <<
$uid1 = getuid_sid($sid);
$mid = $_GET["mid"];
if($page=="" || $page<=0)$page=1;
$noi = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM dave_usergallery WHERE sex='M'"));
$num_items = $noi[0]; //changable
$items_per_page= 5;
$num_pages = ceil($num_items/$items_per_page);
if(($page>$num_pages)&&$page!=1)$page= $num_pages;
$limit_start = ($page-1)*$items_per_page;
$sql = "SELECT uid, id, imageurl FROM dave_usergallery WHERE sex='M' ORDER BY id DESC LIMIT $limit_start, $items_per_page";
///////////////////// part of the added code also.
$result = mysql_query($sql) or die("Error: " . mysql_error());
////
echo "<p>";
$items = mysql_query($sql);
echo mysql_error();
if(mysql_num_rows($items)>0)
{
while ($item = mysql_fetch_array($items))
{
$sql = "SELECT rating FROM dave_usergallery_rating WHERE imageid='".$item[1]."'";
$imginfo = mysql_query($sql);
echo mysql_error();
if(mysql_num_rows($imginfo)>0)
{
while ($imginfos = mysql_fetch_array($imginfo)){
$ratingtotal = $ratingtotal + $imginfos[0];}
}
if($totalcomments<1){$totalcomments=0;}
$norm = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM dave_usergallery_rating WHERE imageid='".$item[1]."'"));
if ($norm[0]>0){
$rating = ceil($ratingtotal/$norm[0]);
}else{$rating=0;}
$rated = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM dave_usergallery_rating WHERE byuid='".$uid1."' and imageid ='".$item[1]."'"));
$totalcomments = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM dave_usergallery_rating WHERE imageid ='".$item[1]."' and commentsyn ='Y'"));
$userinfo = mysql_fetch_array(mysql_query("SELECT name FROM dave_users WHERE id='".$item[0]."'"));
echo "<a href=\"index.php?action=viewuser&amp;who=$item[0]&amp;sid=$sid\"><b><i><u>$userinfo[0]</u></i></b></a><small>";
if(canratephoto($uid1, $item[0]) and ($rated[0]==0))
{
echo "<br/><a href=\"usergallery.php?action=rate&amp;sid=$sid&amp;whoimage=$item[1]\">Rate This Photo</a>";
}
if(ishost(getuid_sid($sid))||((isowner(getuid_sid($sid))||((isheadadmin(getuid_sid($sid)))))))
{
echo " / <a href=\"usergallery.php?action=del&amp;sid=$sid&amp;whoimage=$item[1]\">Del</a>";
}
if($uid1==$item[0])
{
echo " / <a href=\"usergallery.php?action=del&amp;sid=$sid&amp;whoimage=$item[1]\">Del</a>";
}
if($uid1==$item[0])
{
echo "<br/><a href=\"genproc.php?action=upavg&amp;sid=$sid&amp;avsrc=$item[2]\">Use As profile pic</a>";
}

echo "</small><br/><img src=\"$item[2]\" alt=\"$userinfo[0]\"/><br/>";
echo "<small>Rating: $rating/10 (<a href=\"usergallery.php?action=votes&amp;sid=$sid&amp;whoimage=$item[1]\">$norm[0]</a> Votes)<br/><a href=\"usergallery.php?action=comments&amp;sid=$sid&amp;whoimage=$item[1]\">Comments</a>($totalcomments[0])";
echo "</small><br/><a href=\"$item[2]\" alt=\"$userinfo[0]\"/>Download<br/>";
////////////////////////my problem part
while($row = mysql_fetch_array($result)){
echo "<br/><a href=\"getpics.php?file=$row[id]&amp;sid=$sid\">Download test</a>";
<br/>";
}
////////////////////////
echo "</small><br/><br/>";
$ratingtotal = 0;

}
}

echo "</p>";
echo "<p align=\"center\">";
if($page>1)
{
$ppage = $page-1;
echo "<a href=\"usergallery.php?action=males&amp;sid=$sid&amp;page=$ppage\">«Prev</a> ";
}
if($page<$num_pages)
{
$npage = $page+1;
echo "<a href=\"usergallery.php?action=males&amp;sid=$sid&amp;page=$npage\">Next»</a>";
}
echo "<br/>$page/$num_pages<br/>";
if($num_pages>2)
{
$rets = "<form action=\"usergallery.php\" method=\"get\"><p align=\"center\">";

$rets .= "<input name=\"page\" style=\"-wap-input-format: '*N'\" size=\"3\"/>";
$rets .= "<input type=\"hidden\" name=\"sid\" value=\"$sid\"/>";
$rets .= "<input type=\"hidden\" name=\"action\" value=\"$action\"/>";
$rets .= "<input type=\"hidden\" name=\"view\" value=\"$view\"/>";
$rets .= "<input type=\"Submit\" value=\"Go To Page\" Name=\"Submit\"/></form>";

echo $rets;
}
echo "</p>";
////// UNTILL HERE >>
echo "<p align=\"center\">";
echo "<br/><br/><a href=\"usergallery.php?action=main&amp;sid=$sid\">«Back to Gallery</a><br/>";
echo "<a href=\"index.php?action=main&amp;sid=$sid\"><img src=\"images/home.gif\" alt=\"*\"/>";
echo "Home</a>";
echo "</p>";
echo"</body>";
exit();


i have pointed out the bits i have added and tried to work it out.

really hope u guys can help.

Spookster
09-07-2011, 01:48 AM
Your code is hard to read through.

A few suggestions:
1. Comment lines are free and useful so please feel free to use them
2. Why are you using PHP to echo out the entire contents of the html page? Would it not be better to seperate your logic and content from your presentation? You can go in and out of PHP mode and only go into PHP mode when you need to perform an action at that point in the page.
3. Use meaningful and clear names for variables. I shouldn't have to read through many other surrounding lines of code to figure out what 1 line of code means. Comments also help with this.

nclemale
09-07-2011, 02:08 AM
ok i have googled a bit more on this .still havnt found an answer but this may help you ,



$sql = "SELECT uid, id, imageurl FROM usergallery WHERE sex='M' ORDER BY id DESC LIMIT $limit_start, $items_per_page";
////
$result = mysql_query($sql) or die("Error: " . mysql_error());
//////////////////
while($row = mysql_fetch_array($result)){
echo "<a href=\"getpics.php?file=$row[id]&amp;sid=$sid\">Download test</a>";
}


all i need to know is my loop coding correct .as i mentioned above its doing the job fine .
just that its placing all 5 looped links one after another and i cant get them to follow suit with the other looped stuff. i didnt code this btw .just trying to edit it add the download links.

Spookster
09-07-2011, 02:27 AM
That while loop will execute once for each record returned from the database. So yes if you have 5 records returned from the queery you will echo out 5 anchor tags one after the other.

nclemale
09-07-2011, 02:41 AM
ok thanks is there any chance you can help me putting it in the correct place.

as i mentioned previously the output am getting is the 5 links in a list .
when it should run
pic
link
pic
link
pic
link. etc etc etc .

Spookster
09-07-2011, 03:38 AM
That code is nearly intelligible. How the code is structured is just not good. You'd probably be better off just scrapping this code and starting over.

This is how the code should be structured
1. Run query to get images
2. Loop through image query results to print out each image
3. Inside image loop run query to get links for the current image
4. Inside image loop run another loop to loop through link query results to print each link for the current image.

What you have now is you are running your query for links before you ever even loop through the image query result. So I am not even sure how you even expect to know which link goes with which image.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum