RyanSF07 Posted August 28, 2011 Share Posted August 28, 2011 Hello, I'm trying to set a variable based on a word that is pulled out of the DB in an array. What is the correct what of doing this? Currently I'm trying this without success: while($row = mysql_fetch_array($result)) { if ($row['level_text'] = "beginning") { $page = "beginning.php?id="; } elseif ($row['level_text'] = "low_intermediate") { $page = "low_intermediate.php?id="; } elseif ($row['level_text'] = "intermediate") { $page = "intermediate.php?id="; } elseif ($row['level_text'] = "high_intermediate") { $page = "intermediate.php?id="; } if ($tdcount == 1) $content .= "<li><div> <a href=\"$page" . $row['id'] ."\"></a> </div></li>"; } Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/ Share on other sites More sharing options...
voip03 Posted August 28, 2011 Share Posted August 28, 2011 Use Switch and id dose not have value. Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262928 Share on other sites More sharing options...
xyph Posted August 28, 2011 Share Posted August 28, 2011 You've got it right, but you need to use == rather than =. = is an assignment operator. http://www.php.net/manual/en/language.operators.comparison.php Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262929 Share on other sites More sharing options...
RyanSF07 Posted August 28, 2011 Author Share Posted August 28, 2011 thanks .. I'll google Switch. I'm sorry... that was just a snip. The script itself works (the complete script where the id is set) but all "ids" play on the same page. I'm trying to set a "page" variable so that the content doesn't have to play on one set page, rather a "level specific" page base on the row[level_text] Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262930 Share on other sites More sharing options...
RyanSF07 Posted August 28, 2011 Author Share Posted August 28, 2011 I tried this: if ($row['level_text'] == "beginning") { $page == "beginning.php?id="; } elseif ($row['level_text'] == "low_intermediate") { $page == "low_intermediate.php?id="; } elseif ($row['level_text'] == "intermediate") { $page = "intermediate.php?id="; } elseif ($row['level_text'] == "high_intermediate") { $page == "high_intermediate.php?id="; } echo $page; and get: beginning.php?id=beginning.php?id=beginning.php?id=beginning.php?id=beginning.php?id= Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262931 Share on other sites More sharing options...
RyanSF07 Posted August 28, 2011 Author Share Posted August 28, 2011 also, it looks like $row[level_text] has been set to equal "beginning" in other places where that $row echo'd onto the page... Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262932 Share on other sites More sharing options...
RyanSF07 Posted August 28, 2011 Author Share Posted August 28, 2011 now trying this -- on the right track? not working yet... switch ($page) { case label1: if ($row['level_text'] == "beginning") { $page == "beginning.php?id="; } break; case label2: if ($row['level_text'] == "low_intermediate") { $page == "low_intermediate.php?id="; } break; case label3: if ($row['level_text'] == "intermediate") { $page == "intermediate.php?id="; } break; case label4: if ($row['level_text'] == "high_intermediate") { $page == "high_intermediate.php?id="; } } echo $page; Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262934 Share on other sites More sharing options...
flappy_warbucks Posted August 28, 2011 Share Posted August 28, 2011 Why don't you try: switch($row['level_text']) { case "beginning": $level = "beginning"; break; case "low_intermediate": $level = "low_intermediate"; break; case "intermediate": $level = "intermediate"; break; case "high_intermediate": $level = "high_intermediate"; break; default: $level = "unknown"; //incase something goes wrong somewhere. Always plan to for things to go wrong } $page = "levels.php?level=". $level; // do whatever you will afterwoods Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262935 Share on other sites More sharing options...
flappy_warbucks Posted August 28, 2011 Share Posted August 28, 2011 or, if you wanted to be super lazy, you could do: $page = "levels.php?level=". $row['level_text']; Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262936 Share on other sites More sharing options...
RyanSF07 Posted August 28, 2011 Author Share Posted August 28, 2011 looks like that is going to work. I'll keep playing with it. thank you! Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262937 Share on other sites More sharing options...
RyanSF07 Posted August 28, 2011 Author Share Posted August 28, 2011 Actually -- that was close but not spot on... Trying to do more like this: switch($row['level_text']) { case "beginning": $page = "beginning.php?id="; break; case "low_intermediate": $page = "low_intermediate.php?id="; break; case "intermediate": $page = "intermediate.php?id="; break; case "high_intermediate": $page = "high_intermediate.php?id="; break; default: $page = "default.php?id="; //incase something goes wrong somewhere. Always plan to for things to go wrong } echo $page; The echo should produce something like: (there are 5 instances displayed) high_intermediate.php?id= beginning.php?id= beginning.php?id= low-intermediate.php?id= high-intermediate.php?id= Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262938 Share on other sites More sharing options...
flappy_warbucks Posted August 28, 2011 Share Posted August 28, 2011 You have lost me. That switch statement will pull up the page you want going on the information provided by the DB. If you want all the pages pulled up, then remove the "break;" lines and the switch statement will pull up all all files. Also, your '?id=' is empty. If your resulting pages rely on that for whatever reason, it may be a good idea to populate it. Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262940 Share on other sites More sharing options...
flappy_warbucks Posted August 28, 2011 Share Posted August 28, 2011 OK, i have just seen what you want. Delete the break lines, and get rid of default. Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262941 Share on other sites More sharing options...
flappy_warbucks Posted August 28, 2011 Share Posted August 28, 2011 $page[] = "beginning.php?id="; $page[] = "low_intermediate.php?id="; $page[] = "intermediate.php?id="; $page[] = "high_intermediate.php?id="; $display_page = ""; foreach($page as $k=>$v) { $display_page .= $v; } echo $display_page; That will echo out: "high_intermediate.php?id= beginning.php?id= beginning.php?id= low-intermediate.php?id= high-intermediate.php?id=" Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262942 Share on other sites More sharing options...
RyanSF07 Posted August 29, 2011 Author Share Posted August 29, 2011 Thank you Flappy_Warbucks, Sorry for the delayed reply. I have this array pulling 5 items into a table: while($row = mysql_fetch_array($result)) { Those items become links to the content. I'd like to make the first part of the link dynamic, so that the content can display on a level specific page. For example, instead of the 5 items pulled from db linked like this: <a href=\"default-page.php?id=" . $row['id'] ."\"></a> They would be like this: <a href=\"beginning.php?id=" . $row['id'] ."\"></a> <a href=\"low_intermediate.php?id=" . $row['id'] ."\"></a> <a href=\"intermediate?id=" . $row['id'] ."\"></a> <a href=\"high_intermediate?id=" . $row['id'] ."\"></a> .. again depending on the $row[level_text] Does that make sense? Thank you so much for your help with this! Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1262999 Share on other sites More sharing options...
RyanSF07 Posted August 29, 2011 Author Share Posted August 29, 2011 so trying to do something like this: switch($row['level_text']) { case "beginning": $page = "beginning.php?id="; break; case "low_intermediate": $page = "low_intermediate.php?id="; break; case "intermediate": $page = "intermediate.php?id="; break; case "high_intermediate": $page = "high_intermediate.php?id="; break; } The $page variable should be set based on the $row[level_text]. For example, if the $row[level_text] = low_intermediate, the $page variable would equal: low_intermediate.php?id= and the link <a href=\"$page" . $row['id'] ."\"></a> would go to: <a href=\"low_intermediate.php?id=" . $row['id'] ."\"></a> (the id's are working just fine -- it's getting that $page variable to set right that's giving me trouble.) Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1263024 Share on other sites More sharing options...
RyanSF07 Posted August 29, 2011 Author Share Posted August 29, 2011 I was able to get everything to work using string replace. Pulled the level_text from the database and modified it to format the correct, level specific url. Thanks! Ryan Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1263195 Share on other sites More sharing options...
Pikachu2000 Posted August 29, 2011 Share Posted August 29, 2011 Why not just do this? No conditional required at all, unless you need to check that $row['level_text'] has a valid value before proceeding. $page = "<a href=\"{$row['level_text']}.php?id={$row['id']}\">Link text goes here</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/245907-would-i-use-elseif-or-something-other/#findComment-1263198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.