Jump to content

would I use "elseif" or something other?


RyanSF07

Recommended Posts

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>";
   } 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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=

 

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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=

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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="

 

:)

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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.)

 

 

 

 

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.