StudioTime
StudioTime

Reputation: 24019

Pushing variable into function to check if exists - PHP

I have a simple form where a user adds a type of act. Then saves it.

They can then go back and edit it. When they go back to edit I want the type of act they are editing to show as selected in a drop down in the form.

I use a function to print the drop down box and am trying to send the current variable into the function to check and amend the results. Hope that makes sense!

The problem is that the variable $row[act_type] is NOT being seen inside the function

Anyway, here's how I call the function ($row[act_type] DOES exist):

edit_act_types('<?=$row[act_type]?>');

Then to print the drop down I'm using:

function edit_act_types($actType) {
    $mysqli = dbConnect(); // connect with db
    $query_actTypes="SELECT * FROM actTypes order by actType";
    $result_actTypes = $mysqli->query($query_actTypes);

    echo '<select class="form-control" id="type" name="type"><option value="">'.$actType.' Select...</option>';

    while($row_actType = $result_actTypes->fetch_array()){

        if($actType==$row_actType['actTypes_id']) {

            $selected = 'selected="selected"';

        }

        echo '<option value="'.$row_actType['actTypes_id'].'" '.$selected.'>'.$row_actType['actType'].'</option>';

    } 

    echo '</select>';
};

Upvotes: 0

Views: 59

Answers (2)

Amal
Amal

Reputation: 76666

You have the following line in your code:

edit_act_types('<?=$row[act_type]?>');

I'm not sure what you're trying to achieve with the above line. The <?=$row[act_type]?> part will just be considered as a string and be passed as your function parameter instead of the actual variable. You don't need to use PHP tags when calling the function.

Consider the following piece of code:

header('Content-Type: text/plain');

function foo($bar) {
    echo $bar;
}

$var = 'hello';
foo('<?=$var?>');

The output will be <?=$var?>, not hello.

So, your function call should look like:

edit_act_types($row['act_type']);

Read more about Functions in the PHP Manual.

Upvotes: 1

vishal shah
vishal shah

Reputation: 222

The function is called in php tag

<?php   
 edit_act_types($row[act_type]);
?>

So <?=$row[act_type]?> -- will not work

Upvotes: 1

Related Questions