A different way [help]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
affihq
Forum Newbie
Posts: 7
Joined: Wed Sep 27, 2006 12:06 am

A different way [help]

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post 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

?>
Last edited by visonardo on Mon Oct 02, 2006 10:06 pm, edited 1 time in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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());
Post Reply