Page 1 of 1

A different way [help]

Posted: Mon Oct 02, 2006 8:36 pm
by affihq
Is there a better way I can do this that won't make such a massive amount of code to do the same thing?

Code: Select all

$data = mysql_query("SELECT * FROM gallery") or die(mysql_error()); 
while($row = mysql_fetch_array( $data ))
	{ 
	if ($row['user']==$user AND $row['gallery']==1)
		{ 
		if ($file1=="")
			{
			$file1="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[1]=$row['caption'];
			}
		elseif ($file2=="" AND $file1!="")
			{
			$file2="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[2]=$row['caption'];
			}
		elseif ($file3=="" AND $file2!="" AND $file1!="")
			{
			$file3="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[3]=$row['comment'];
			}
		elseif ($file4=="" AND $file2!="" AND $file1!="" AND $file3!="")
			{
			$file4="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[4]=$row['caption'];
			}
		elseif ($file5=="" AND $file2!="" AND $file1!="" AND $file3!="" AND $file4!="")
			{
			$file5="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[5]=$row['caption'];
			}
		elseif ($file6=="" AND $file2!="" AND $file1!="" AND $file3!="" AND $file4!=""  AND $file5!="")
			{
			$file6="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[6]=$row['caption'];
			}
		elseif ($file7=="" AND $file2!="" AND $file1!="" AND $file3!="" AND $file4!=""  AND $file5!="" AND $file6!="")
			{
			$file7="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[7]=$row['caption'];
			}
		elseif ($file8=="" AND $file2!="" AND $file1!="" AND $file3!="" AND $file4!=""  AND $file5!="" AND $file6!="" AND $file7!="")
			{
			$file8="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[8]=$row['caption'];
			}
		elseif ($file9=="" AND $file2!="" AND $file1!="" AND $file3!="" AND $file4!=""  AND $file5!="" AND $file6!="" AND $file7!="" AND $file8!="")
			{
			$file9="../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[9]=$row['caption'];
			}
		}
	}
any help is much appreciated, thanks!

Posted: Mon Oct 02, 2006 9:11 pm
by Christopher
I don't think you need any of the AND $fileX != '' checks because the preceeding ifs/elseifs excludes those cases. You could probably put the $file into an array parallel to the $comments and loop through the $files.

Posted: Mon Oct 02, 2006 10:04 pm
by visonardo
first, do an array with $file# content where the index will be their nema. For example:

Code: Select all

<?

$files['file1']=$file1;
$files['file2']=$file2;
$files['file3']=$file3;
$files['file4']=$file4;
$files['file5']=$file5;
$files['file6']=$file6;
$files['file7']=$file7;
$files['file8']=$file8;
$files['file9']=$file9;


//and then scan it using foreach function

?>

Posted: Mon Oct 02, 2006 10:04 pm
by Zoxive
Maybe something like...

Code: Select all

$data = mysql_query("SELECT * FROM gallery") or die(mysql_error());
while($row = mysql_fetch_array( $data )){
   if ($row['user']==$user AND $row['gallery']==1){
	  for($i=1;$i<=9;$i++){
	  	if(empty(${'file' . $i})){
			${'file' . $i} = "../userfiles/" . $row['folder'] . "/" . $row['photo'];
			$comment[$i]=$row['caption'];
			
		}
	}
}
-NSF

Posted: Mon Oct 02, 2006 11:03 pm
by John Cartwright
could lose the if ($row['user'] ... ) { .. and simply add that to where sql where clause

Code: Select all

$data = mysql_query("SELECT * FROM gallery WHERE `user` = \''.mysql_real_escape_string($user).'\' AND `gallery` = 1") or die( mysql_error());