Jump to content

alternate starts for a while statement using an if statement - Solved with thanks


niche
 Share

Recommended Posts

I need to start a while statement depending on the outcome of two queries. I thought I could use an if statement to use the correct first line of the while statement, but this produces a parse error:

<?phpinclude "connect_to_mysql.php";$plan = mysql_query("SELECT * FROM plan WHERE cliorder <> '' AND TIMESTAMP(NOW()) BETWEEN TIMESTAMP(begin) AND TIMESTAMP(end) ORDER BY cliorder DESC") or die(mysql_error()); $keyword = "Aircraft";include_once "connect_to_mysql.php";$keyword2 = mysql_query("SELECT * FROM stk INNER JOIN plan ON stk.cliorder=plan.cliorder WHERE stk.subcategory LIKE  '%" . $keyword . "%'") or die(mysql_error()); if (mysql_num_rows($keyword2) == 3) {  while ($plan2 = mysql_fetch_array($keyword2)) {	 } else {	 while ($plan2 = mysql_fetch_array($plan)) {}echo $plan2['cliorder'] . '<br/>';}?>

If I can't use an if statement to select the correct first line, what should I use?

Edited by niche
Link to comment
Share on other sites

Copy the entire loop:

if(mysql_num_rows($keyword2) == 3) {    while ($plan2 = mysql_fetch_array($keyword2)) {	    echo $plan2['cliorder'] . '<br/>';    }} else {    while ($plan2 = mysql_fetch_array($plan)) {	    echo $plan2['cliorder'] . '<br/>';    }}

Link to comment
Share on other sites

It's not a great idea to have it run both queries if you're only going to use one. Run the first one, validate the result, run the second one if necessary (and save the result to the same variable as the first result), then do a single while loop to loop over the result set (which has the same variable name regardless of which query it came from). That way you don't have to duplicate the code in the while loop. The syntax error is because your code is structured like this:

if (keyword2...){  while (plan2...) {}  else  {	while (plan2...) {}  }}

So the error is because that else does not correspond to an if statement, it is inside the if statement.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...