Jump to content

Error in code, trying to Loop!


essjay_d12

Recommended Posts

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_ELSE

Can someone see why?

Thanks

d
Link to comment
Share on other sites

[!--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_ELSE

Can someone see why?

Thanks

d
[/quote]


It seems like you're not encapsulating the if and else statements in { ... }, them squiggly things
Link to comment
Share on other sites

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
Share on other sites

It runs the code - but I do not get expected value

Say $row[favourites] = Bread, Milk, Water

then 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 thanks

d
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.