if statements within a value?

Discussion in 'PHP' started by mokimofiki, Aug 27, 2008.

  1. #1
    I need to place an if statement as a value of a selection dropdown. I can do it fine when there is static options but when I have the options populate based on a table in mySQL I cannot figure out how to add the if statement again.

    Really what i'm looking for is someone to maybe clean up the code I have below so that the if statement does what it should.

    <?php
    $result = mysql_query("SELECT description FROM sppkg WHERE optiontype='special'");
    print "<select name=sppkgspecial> \n";
    print "<option></option>";
    print "<option value="if (isset($_SESSION['sppkgspecial'])) {echo "$_SESSION[sppkgspecial]";} "> if (isset($_SESSION['sppkgspecial'])) {echo "$_SESSION[sppkgspecial"];} </option>"
    while($row = mysql_fetch_assoc($result))
    { print "<option value=\"{$row['description']}\">{$row['description']}</option> \n"; }
    print "</select> \n";
    ?>

    I know the highlighted line is wrong I just am not sure how to fix it?
     
    mokimofiki, Aug 27, 2008 IP
  2. themaster

    themaster Well-Known Member

    Messages:
    934
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    140
    #2
    print "<option value=\"if (isset($_SESSION['sppkgspecial'])) {echo "$_SESSION[sppkgspecial]";} \"> if (isset($_SESSION['sppkgspecial'])) {echo "$_SESSION[sppkgspecial"];} </option>"
     
    themaster, Aug 27, 2008 IP
  3. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    
    <?php
    	$result = mysql_query("SELECT description FROM sppkg WHERE optiontype='special'");
    
    	print "<select name=sppkgspecial> \n";
    	print "<option value=";
    
    	if(isset($_SESSION['sppkgspecial'])) 
    	{
    		echo $_SESSION[sppkgspecial] . ">" . $_SESSION[sppkgspecial] . "</option>";;
    	}  
    
    	while($row = mysql_fetch_assoc($result))
    	{ 
    		print "<option value=\"{$row['description']}\">{$row['description']}</option> \n"; 
    	}
    
    	print "</select> \n"; 
    ?>
    
    PHP:
    That should work, although haven't tested it as i can't access PHP at the mo.

    Regards,

    Steve
     
    Steve136, Aug 27, 2008 IP
    mokimofiki likes this.
  4. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Thank you Steve136 what you have above seems to work very well but now I have another question (of course :) )

    if it says 12' 10" Wide beam in the database then it shows 12' 10" Wide beam in the drop down but when the session variable is called it only shows 12' 10 and nothing else any thoughts?
     
    mokimofiki, Aug 27, 2008 IP
  5. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,

    Glad it worked. The session won't save the " character unless it is escaped, which if you are inserting a " into a session it needs to be done like "\

    Example code below of inserting " into a session variable.

    
    <?php
    
    	$_SESSION["test"] = "12' 10\"";
    
    	echo $_SESSION["test"];
    ?>
    
    PHP:
    Which will output:
    
    12' 10"
    
    Code (markup):
    Hope that helps,

    Regards,

    Steve
     
    Steve136, Aug 27, 2008 IP
  6. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #6
    So if there is a " in a database entry remove it so that when pulling from the database it doesn't ignore everything after it?

    12' 10" wide .. in database
    12' 10" wide .. in option list
    12' 10 .. in session variable


    12' 10 wide .. in database
    12' 10 wide .. in option list
    12' 10 wide .. in session variable


    Just a recap tp be sure I understand....

    Thank you for your help it has saved me alot of frusterations :)
     
    mokimofiki, Aug 27, 2008 IP
  7. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,

    I believe it's just in PHP that it will ignore it (unless it is escaped) I would remove the " in the database entry and add them in manually when displaying, however if you do want to use them there is a PHP function that will escape strings for you so they can be displayed properly.

    http://uk3.php.net/manual/en/function.mysql-escape-string.php

    Regards,

    Steve
     
    Steve136, Aug 27, 2008 IP