Jump to content

Any help please


philbob
Go to solution Solved by QuickOldCar,

Recommended Posts

Hi to you all can i say am very very new to php, and any help would be appreciated thanks.

 

I keep getting this error  Parse error: syntax error, unexpected 'else' (T_ELSE)

if ($xcatname) $resa = mysql_query("SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '".$xcatname."'");
$rowa = mysql_fetch_array($resa);
$desc_msg = $rowa['catdesc'];
 {
	echo '<div id="showad-textbox">'.$rowa['catdesc'].'</div><br>';
 }
 elseif($xsubcatname) $resa = mysql_query("SELECT subdesc FROM `$t_subcats` WHERE `subcatname` LIKE '".$xsubcatname."'");
$rowa = mysql_fetch_array($resa);
$desc_msg = $rowa['subdesc'];
 {
        echo '<div id="showad-textbox">'.$rowa['subdesc'].'</div><br>';
 }

If i change the elseif  to if  then no error but i see both text descriptions  any help thanks phil

Link to comment
Share on other sites

You have a malformed if/else syntax, The lines highlighted in red need to be within the braces { }

 

if ($xcatname) $resa = mysql_query("SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '".$xcatname."'");
$rowa = mysql_fetch_array($resa);
$desc_msg = $rowa['catdesc'];

{
echo '<div id="showad-textbox">'.$rowa['catdesc'].'</div><br>';
}
elseif($xsubcatname) $resa = mysql_query("SELECT subdesc FROM `$t_subcats` WHERE `subcatname` LIKE '".$xsubcatname."'");
$rowa = mysql_fetch_array($resa);
$desc_msg = $rowa['subdesc'];

{
        echo '<div id="showad-textbox">'.$rowa['subdesc'].'</div><br>';
}

Edited by Ch0cu3r
Link to comment
Share on other sites

Thanks you for your quick reply I tried 

if ($xcatname $resa = mysql_query("SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '".$xcatname."'");
$rowa = mysql_fetch_array($resa);
$desc_msg = $rowa['catdesc']; )
 {
	echo '<div id="showad-textbox">'.$rowa['catdesc'].'</div><br>';
 }
 elseif($xsubcatname $resa = mysql_query("SELECT subdesc FROM `$t_subcats` WHERE `subcatname` LIKE '".$xsubcatname."'");
$rowa = mysql_fetch_array($resa);
$desc_msg = $rowa['subdesc']; )
 {
        echo '<div id="showad-textbox">'.$rowa['subdesc'].'</div><br>';
 }

but now getting error syntax error, unexpected '$resa' (T_VARIABLE) in your code on line 1  Phil

Link to comment
Share on other sites

WHAT!? How did you come up with that? Was my post not clear enough?

Sorry mis read post, still having same error syntax error, unexpected 'elseif' (T_ELSEIF) in your code on line 9

if ($xcatname) {$resa = mysql_query("SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '".$xcatname."'");}
{$rowa = mysql_fetch_array($resa);}
{$desc_msg = $rowa['catdesc'];}

{
	echo '<div id="showad-textbox">'.$rowa['catdesc'].'</div><br>';
}

elseif($xsubcatname) {$resa = mysql_query("SELECT subdesc FROM `$t_subcats` WHERE `subcatname` LIKE '".$xsubcatname."'");}
{$rowa = mysql_fetch_array($resa);}
{$desc_msg = $rowa['subdesc'];}

{
        echo '<div id="showad-textbox">'.$rowa['subdesc'].'</div><br>';
}

thanks again for your help

Link to comment
Share on other sites

  • Solution

Could do with two if's and actually use the variable are setting, then echo after

if($xcatname) {

    $query = "SELECT catdesc FROM `$t_cats` WHERE `catname` = '".$xcatname."'";

        if ($resa = mysql_query($query)) {

            while($rowa = mysql_fetch_assoc($resa)){
                $desc_msg = $rowa['catdesc'];
            }

        }

}


if($xsubcatname) {

    $query = "SELECT subdesc FROM `$t_subcats` WHERE `subcatname` = '".$xsubcatname."'";

        if ($resa = mysql_query($query)) {

            while($rowa = mysql_fetch_assoc($resa)){
                $desc_msg = $rowa['subdesc'];
            }

        }

}

if($desc_msg){

echo '<div id="showad-textbox">'.$desc_msg.'</div><br>';

}

Shouldn't use the outdated mysql_ functions and use mysqli_ or pdo

 

Should also escape or use prepared statements for anything inserted into queries.

 

Another thing is that when you fetch an array it returns both the numerical index and associative array, double the data. If you do not need the numerical array use mysqli_fetch_assoc

 

There are example codes there can look over.

 

Any reason are using LIKE?

I would assume your categories are specific since not doing a while loop, in that case can just use an = versus LIKE

 

If using LIKE for searching...

Firstly would be using a while loop for multiple results.

% are for wildcards

If did have categories with similar words would want to place a % in front and after the term so can find any similar that at least contains the term.

"SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '%".$xcatname."%'"

"SELECT subdesc FROM `$t_subcats` WHERE `subcatname`LIKE '%".$xsubcatname."%'"

 

I personally like using full-text search in boolean mode,

When you get to using multiple where/and , then also multiple columns, full-text simplifies the queries and additionally makes for very specific searches.

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.