Home > Software engineering >  php array within function only 1 result
php array within function only 1 result

Time:10-01

I can not figure out how to get $output working within this function.

    <?php
$conn = new mysqli($servername, $username, $password, $dbname);
function fill_unit_select_box($conn)
    { 
     $output = '';
     $query = "SELECT * from `skater` ORDER By `skater`.`skater_name_first` ASC";
     $result = $conn->query($query);
    
     while($row = mysqli_fetch_array($result))
     {
        $output .= '<option value="'.$row["skater_NUM"].'">'.$row["skater_NUM"].''.$row["skater_NUM"].'</option>';
      //$output .= '<option value="'.$row["skater_NUM"].'">'.$row["skater_name_first"].''.$row["skater_name_last"].'</option>';
 }
 return $output;
}

?>

Function is called later.

<option value="">Select Unit</option><?php echo fill_unit_select_box($conn); ?></select>

If I use the following nothing works. The option fields are not displayed in the select.

output .= '<option value="'.$row["skater_NUM"].'">'.$row["skater_name_last"].''.$row["skater_name_first"].'</option>';

If I use the following the function works.

output .= '<option value="'.$row["skater_NUM"].'">'.$row["skater_NUM"].''.$row["skater_NUM"].'</option>';

Calling anything except skater_NUM causes issues.

If I call the function outside of the following the function works.

 $(document).on('click', '.add', function(){
  var html = '';
  html  = '<tr>';
  html  = '<td><input type="text" name="item_name[]"  /></td>';
  html  = '<td><input type="text" name="item_quantity[]"  /></td>';
  html  = '<td><select name="item_unit[]" ><option value="">Select Unit</option><?php echo fill_unit_select_box($conn); ?></select></td>';
  html  = '<td><button type="button" name="remove" ><span ></span></button></td></tr>';
  $('#item_table').append(html);
 });

The function is working. Just the add row .add button will not add new rows with anything other then numbers for skater_NUM field.

 $(document).on('click', '.add', function(){
  var html = '';
  html  = '<tr>';
  html  = '<td><input type="text" name="item_name[]"  /></td>';
  html  = '<td><input type="text" name="item_quantity[]"  /></td>';
  html  = '<td><select name="item_unit[]" ><option value="">Select Unit</option><option value="8">Skater1</option><option value="21">Skater2</option></select></td>';
  html  = '<td><button type="button" name="remove" ><span ></span></button></td></tr>';
  $('#item_table').append(html);
 });

CodePudding user response:

the variable $output is replaced in every loop, $output will give you the result of the last row. Solution try to replace $output by $output .=

CodePudding user response:

I figured it out... add ".." Now the echo likes characters other then numbers.... This took was too long.

html  = '<td><select name="item_unit[]" ><option value="">Select Unit</option><?php echo fill_unit_select_box($conn); ?></select></td>';

to

html  = '<td><select name="item_unit[]" ><option value="">Select Unit</option>"<?php echo fill_unit_select_box($conn); ?>"</select></td>';
  • Related