0

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

Edited by pritaeas: Moved.

5
Contributors
9
Replies
36
Views
4 Years
Discussion Span
Last Post by pritaeas
0

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.

0

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.

0

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.

0

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?

0

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

0

@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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.