1

code snippet from html_form_class

<?php
$frmStr = $frm->addSelectList(
                      'city',
                      $city,
                      true,
                      '',
                      '--- Select City ---',
                      array(
                          'class' => 'dropdown-style5',
                          'id' => 'city')); 
echo $frmStr; ?>

code snippet from seachcar.php

   $city = $db->select('City','City_Name');
foreach($city as $row)
{
 $row;
}

"Array" is displaying in dropdown instead of values fetched from database Please Advice!

function addSelectList($name, $option_list, $bVal = true, $selected_value = NULL,
            $header = NULL, $attr_ar = array() ) {
        $str = "<select name=\"$name\"";
        if ($attr_ar) {
            $str .= $this->addAttributes( $attr_ar );
        }
        $str .= ">\n";
        if ( isset($header) ) {
            $str .= "  <option value=\"\">$header</option>\n";
        }
        foreach ( $option_list as $val => $text ) {
            $str .= $bVal? "  <option value=\"$val\"": "  <option";
            if ( isset($selected_value) && ( $selected_value === $val || $selected_value === $text) ) {
                $str .= $this->xhtml? ' selected="selected"': ' selected';
            }
            $str .= ">$text</option>\n";
        }
        $str .= "</select>";
        return $str;
    }

html output of addSelectList function is

  <select name="city" class="dropdown-style5" id="city">
  <option value="">--- Select City ---</option>
  <option value="0">Array</option>
  <option value="1">Array</option>
  <option value="2">Array</option>
  <option value="3">Array</option>
9
  • Can we see all your code? I can't tell a damn thing without seeing the whole addSelectList() function. If you're using a framework or something, what is it? Commented Jul 23, 2013 at 22:45
  • Dude, really? Edit your post and put that stuff in there so its readable. Commented Jul 23, 2013 at 22:55
  • @searsaw i did it for you now please check and let me know Commented Jul 23, 2013 at 23:00
  • Please tell us the outputted HTML (the value of $frmStr) Commented Jul 23, 2013 at 23:02
  • Again, put it in your question. Commented Jul 23, 2013 at 23:05

3 Answers 3

2

You need to rebuild the array of cities:

$city = $db->select('City','City_Name');
$city_rebuild = array();
foreach($city as $row) {
    $city_rebuild[] = $row['City_Name'];
}
$city = $city_rebuild;
Sign up to request clarification or add additional context in comments.

2 Comments

blank output as well as its my generic class function to hard code it wont work for me at other places
Okay I took that in consideration. Edited my answer.
0

You do echo of array. Something is wrong in your abstraction objects. You must iterate on array to show up its values.

Comments

0

function addSelectList creates a "dropdown" (actually a select element)

You need to remove the html from the function output.

Edit 1 I was confused as to what you were going for. In your foreach($option_list... you need to know what keys are available in the $option_list array and what you want to appear in the select dropdown.

6 Comments

@Mubbashir... I misunderstood, but I know know what you're trying to do. First we need to know what the values available in the array are. Do something like print_r($option_list); and let me know what it says.
values are 'Lahore', 'Karachi', 'Multan','Hyderabad'
That's not what print_r returns. We need to be able to see the structure of the array
I've solved my problem thankyou so very much for anticipation :)
Please post your solution so we can benefit from it. Thats the idea of this community.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.