Page 1 of 1

if statement in concatenation throws error...

Posted: Tue Sep 16, 2008 5:52 am
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...

Re: if statement in concatenation throws error...

Posted: Tue Sep 16, 2008 6:08 am
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...';

Re: if statement in concatenation throws error...

Posted: Tue Sep 16, 2008 6:21 am
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>";