essjay_d12 Posted May 31, 2006 Share Posted May 31, 2006 Can someone see if they can see where this code is going wrong?I'm trying to explode a variable $genre then each value in the new array needs to be put back into an SQL query - where i attempted to insert a new variable ($finalString) into the sql statement.[code]if (isset($_SESSION['username'])) {$genre = $_GET['genre']; // retrieve passed variable//open connection$conn = mysql_connect("localhost", "admin", "adm1n");mysql_select_db("project",$conn);$sql = "select USERNAME, PASSWORD, F_NAME, FAVOURITES, ADMIN, DOB from users where USERNAME = '$_SESSION[username]'";$check = mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_array($check)){$genre = ($row['FAVOURITES']);$genre2 = explode(', ', $genre);}$count = count($genre2);$i=0;$finalString = NULL;do{if (i > 0)$String = " OR F_TYPE LIKE '%";$finalString = $String + $genre2[$i];$String2 = "%'";$finalString = $finalString + $String2;$i++;else$String = "F_TYPE LIKE '%";$finalString = $String + $[i];$String2 = "%'";$finalString = $finalString + $String2;$i++;//echo "The number is " . $i . "<br />";}while ($i<$count);$query = "SELECT ID_NO, F_NAME, F_DIRECTOR, F_REVIEWER, F_DATETIME, F_TYPE, F_SUMMARY, F_REVIEW, F_SCORE FROM films WHERE $finalString ORDER BY F_DATETIME LIMIT 1, 20 ";[/code]At the moment its picking out an error at 'else'...Parse error: parse error, unexpected T_ELSECan someone see why?Thanksd Link to comment https://forums.phpfreaks.com/topic/10843-error-in-code-trying-to-loop/ Share on other sites More sharing options...
lelnguye Posted May 31, 2006 Share Posted May 31, 2006 [!--quoteo(post=378652:date=May 31 2006, 05:24 AM:name=Essjay_d12)--][div class=\'quotetop\']QUOTE(Essjay_d12 @ May 31 2006, 05:24 AM) [snapback]378652[/snapback][/div][div class=\'quotemain\'][!--quotec--]Can someone see if they can see where this code is going wrong?I'm trying to explode a variable $genre then each value in the new array needs to be put back into an SQL query - where i attempted to insert a new variable ($finalString) into the sql statement.[code]if (isset($_SESSION['username'])) {$genre = $_GET['genre']; // retrieve passed variable//open connection$conn = mysql_connect("localhost", "admin", "adm1n");mysql_select_db("project",$conn);$sql = "select USERNAME, PASSWORD, F_NAME, FAVOURITES, ADMIN, DOB from users where USERNAME = '$_SESSION[username]'";$check = mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_array($check)){$genre = ($row['FAVOURITES']);$genre2 = explode(', ', $genre);}$count = count($genre2);$i=0;$finalString = NULL;do{if (i > 0)$String = " OR F_TYPE LIKE '%";$finalString = $String + $genre2[$i];$String2 = "%'";$finalString = $finalString + $String2;$i++;else$String = "F_TYPE LIKE '%";$finalString = $String + $[i];$String2 = "%'";$finalString = $finalString + $String2;$i++;//echo "The number is " . $i . "<br />";}while ($i<$count);$query = "SELECT ID_NO, F_NAME, F_DIRECTOR, F_REVIEWER, F_DATETIME, F_TYPE, F_SUMMARY, F_REVIEW, F_SCORE FROM films WHERE $finalString ORDER BY F_DATETIME LIMIT 1, 20 ";[/code]At the moment its picking out an error at 'else'...Parse error: parse error, unexpected T_ELSECan someone see why?Thanksd[/quote]It seems like you're not encapsulating the if and else statements in { ... }, them squiggly things Link to comment https://forums.phpfreaks.com/topic/10843-error-in-code-trying-to-loop/#findComment-40510 Share on other sites More sharing options...
Honoré Posted May 31, 2006 Share Posted May 31, 2006 This should solve the missing { and } :[code]do{if (i > 0) {$String = " OR F_TYPE LIKE '%";$finalString = $String + $genre2[$i];$String2 = "%'";$finalString = $finalString + $String2;$i++;} else {$String = "F_TYPE LIKE '%";$finalString = $String + $[i];$String2 = "%'";$finalString = $finalString + $String2;$i++;}//echo "The number is " . $i . "<br />";}[/code]and then you have to correct also the following line:[code]$finalString = $String + $[i];[/code] Link to comment https://forums.phpfreaks.com/topic/10843-error-in-code-trying-to-loop/#findComment-40511 Share on other sites More sharing options...
essjay_d12 Posted May 31, 2006 Author Share Posted May 31, 2006 It runs the code - but I do not get expected valueSay $row[favourites] = Bread, Milk, Waterthen at the end of the loop $finalString should be F_TYPE LIKE '%Bread%' OR F_TYPE LIKE '%Milk%' OR F_TYPE LIKE '%Water%'but when i echo $finalString it echoes '0' !!!this is the code again ....[code]while ($row = mysql_fetch_array($check)){$genre = ($row['FAVOURITES']);}$genre2 = explode(', ', $genre);$count = count($genre2);$i=0;$finalString = NULL;do{if (i > 0){$String = " OR F_TYPE LIKE '%";$newString = $String + $genre2[i];$String2 = "%'";$finalString = $finalString + $String2;$i++;}else{$String = "F_TYPE LIKE '%";$newString = $String + $genre2[i];$String2 = "%'";$finalString = $finalString + $String2;$i++;}}while ($i<$count);echo $finalString;[/code]Please help thanksd Link to comment https://forums.phpfreaks.com/topic/10843-error-in-code-trying-to-loop/#findComment-40513 Share on other sites More sharing options...
Honoré Posted May 31, 2006 Share Posted May 31, 2006 replace[code]if (i > 0)[/code]by[code]if ($i > 0)[/code]The following loop will probably get you on the right track:[code]while ($i<$count){ if ($i > 0) { $finalString = $finalString . " OR F_TYPE LIKE '%" . $genre2[i] . "%'"; } else { $finalString = $finalString . "F_TYPE LIKE '%" . $genre2[i] . "%'"; } $i++;}[/code] Link to comment https://forums.phpfreaks.com/topic/10843-error-in-code-trying-to-loop/#findComment-40523 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.