I have problem with this drop down menu. This code is part of form.php and is used by 2 pages, 1 page is working fine in which it will not show the "-assigned-", where the other page is showing it. I don't know why they both display differently.

assuming library is in first lines code above is:

<p><?php $currCategory = $sel_categ['cat_name']; ?>
<strong>CategoryType2:</strong> <select name="select_cat">
<?php if(!$currCategory) { ?>
     <!-- HIDDEN TO TEST BUG -->
     <!--  <option selected >--Unassigned--</option> -->
     <!-- '<option value="'.$catname.'">'.$catname.'</option>'; -->
     <option value="'.$catname.'">-assigned-<?php $catname?></option>
     <?php } ?>

<?php $categories = get_all_categs();
    while($rowArr = mysql_fetch_assoc($categories)){
   $selected = $rowArr['cat_name']==$currCategory?'selected':'';
       echo "<option ".$selected." id='".$rowArr['category_id']."'>".$rowArr['cat_name']."</option>";   
       } ?>
    </select>
 </p>

my function:

function find_selected_filepage() {
    global $sel_categ;
    global $sel_filepage;
    if (isset($_GET['catg'])) {
        $sel_categ = get_categ_by_id($_GET['catg']);
        $sel_filepage = NULL;
    } elseif (isset($_GET['filepage'])) {
        $sel_categ = NULL;
        $sel_filepage = get_filepage_by_id($_GET['filepage']);
    } else {
        $sel_categ = NULL;
        $sel_filepage = NULL;
    }
}

*NOTE: This article should be in PHP, my apologies

Member Avatar for diafol

Avoid globals, use parameters in your functions.
Also, functions usually return something - but not always. Yours lends itself to return something IMO.

<option value="'.$catname.'">-assigned-<?php $catname?></option>

That's well and truly mashed. It's supposed to be plain html, so if you want to include any php, you need to use open and close tags - including a ';' at the end of a php statement.

<option value="<?php echo $catname;?>">-assigned-<?php echo $catname;?></option>

Should work. BUT, you've got so much php/html mixed up that maintaining this looks like a nightmare. Maybe think of using a template or at least try to separate php and html.

thanks diafol for your reply.
i have some few questions thou, if you dont mind.

can someone please explain what is this:

$sel_categ['cat_name'];

cat_name is column name of my category table

on top of the code there should be this:

<?php if (!isset($new_filepage)) {$new_filepage = false;} ?>

what is purpose of this ==$currCategory?'selected':''; in?

$selected = $rowArr['cat_name']==$currCategory?'selected':'';

my goal is:
1. i have a pagefile with current category and what to edit using dropdown menu and select for its new category.

some of these i got on the web.
Thanks.

Yo can take help of ajax and javascript to resolve the issue.

Member Avatar for diafol

So this isn't your code? :(

can someone please explain what is this:
$sel_categ['cat_name'];
cat_name is column name of my category table

This looks as though it's the fetched data from a SELECT query.

on top of the code there should be this:
<?php if (!isset($new_filepage)) {$new_filepage = false;} ?>
what is purpose of this ==$currCategory?'selected':''; in?
$selected = $rowArr['cat_name']==$currCategory?'selected':'';

The last sentence is ternery conditional syntax. It's a short form for if...else...

$variable = (conditional statement) ? true value : false value;

Which is equivalent to:

if(conditional statement){
    $variabe = true value;
}else{
    $variable = false value;
}

This ternery syntax is common to many programming languages, including javascript.

thank you ellena980 and diafol, you are truly wise. i wish i can be an expert PHP so quick, i need to do more projects. any advise?

Member Avatar for diafol

Any advice about what?

stay away from projects until you have a good understanding of the code that you are doing, or using from other places.

@squidge, im using lynda.com basic php mysql CMS template by kevin, then i start from there

@diafol, advice for PHP learning strategy?

I've been looking for such things on the net, but it always brings me back to my self study and own pace.

I hope there will be PHP learning track just like codecademy but with more library of samples and scripts that i can compile and schedule guide that is dynamic.

i guess there's no book yet that really is a standard for learning PHP that helps you become a pro in 3-6 months? if there is we all be very happy engineers. cheers!

advice for PHP learning strategy

Practice, practice, practice and php.net.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.