if statement in concatenation throws error...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arunkar
Forum Commoner
Posts: 50
Joined: Mon Feb 25, 2008 10:37 pm

if statement in concatenation throws error...

Post by arunkar »

Hi Guys,

I wrote the below function to display the data from database on the list/select menu. This is a update page, where the old data from the database is pre-populated in the HTML fields.

Code: Select all

 
function display_mode_list($dbdata,$mod,$tableName,$lookupID,$lookupData) {
    if ($mod=="del"){
        if($dbdata==NULL || $dbdata=='')    $dispVal="<font color='#CBCBCB'>N.A.</font>";
        else $dispVal=$dbdata;
    }
    if ($mod=="edi"){
 
    $queryDyn=mysql_query("SELECT * FROM $tableName");
    $option="<select name='$tableName' id='$tableName'>";
    
    while($fieldsDyn = mysql_fetch_array($queryDyn)){
        $option= $option."<option value='$fieldsDyn[$lookupID]'>$fieldsDyn[$lookupData]</option>";
    }
    $option = $option. "</select>";
        if($dbdata==NULL || $dbdata=='')    $dispVal="$dbdata";
        else $dispVal= $option;
    }
    return $dispVal;
    
}
 
The above function works fine. When I try to pre-select the old data in the below function I'm getting error (actually the page is blank without any error even when I have
ini_set('display_errors','1');
error_reporting(E_ERROR | E_WARNING | E_PARSE);
on top of the page)...
This is the line where I'm doing a if statement to check for old value...

Code: Select all

 
        $option= $option."<option value='$fieldsDyn[$lookupID]'."if($dbdata==$fieldsDyn['$lookupData']){echo 'selected=selected';}".>$fieldsDyn[$lookupData]</option>";
 
 
Any suggestions to overcome this issue?

thanks folks...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: if statement in concatenation throws error...

Post by s.dot »

You cannot concatenate an if() statement.

You'll want to terminate the command with a ; (semi-colon) and then start again on the next line.

Code: Select all

$var = $var . 'something...';
if ($this)
{
     $var .= $that
}
 
$var .= 'some more stuff here...';
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
arunkar
Forum Commoner
Posts: 50
Joined: Mon Feb 25, 2008 10:37 pm

Re: if statement in concatenation throws error...

Post by arunkar »

I've split it,

but I still get a blank page displayed...

any Suggestions?

Code: Select all

        $option= $option."<option value='$fieldsDyn[$lookupID]'";
        
        if($dbdata==$fieldsDyn['$lookupData']){
            $option= $option."selected=selected";
        }
        
        $option= $option.">$fieldsDyn[$lookupData]</option>";
 
Post Reply