how to display the array values separated by (,) form city_place table

Recommended Answers

All 4 Replies

In code? Then show your code. You'll need an implode()

If you want to do this in your query, have a look at GROUP_CONCAT()

i uploaded values by using this code.and i want to display city values where city_place.state_id='11' and city_place.spaid='422'
tables
1.city_place
2.cities
3.states

$cityid = trim(ucwords(strtolower(mysql_real_escape_string($_POST['city']))));
            $num=count($_POST["place"]);

                $ids = array();
                foreach($_POST['place'] as $val)
                {
                $ids[] = (int) $val;
                }
                $ids = implode(',', $ids);
                $sql3 = mysql_query("INSERT INTO `city_place`(`state_id`,`city_id`,`spaid`) VALUES ('$cityid','$ids',$lid)");
Member Avatar for diafol

storing data as a list in a single field is not a good idea. You should create a link table.

Also your insert query looks very odd.

Why is city_id (an integer in your tables) a string from $_POST['city']?
What's the point of $num? It's not used.

WHy is state_id (I assume an integer) getting city_id (which I assume is a string).

after uploading i need to display selected checkbox values based on state_id.

<tr><td><div align="left">City : </div></td><td>
                <?php $sql4 = mysql_query("SELECT `state_id`, `state` FROM `states` "); ?>
                <select name="city" id="city" onChange="getPlace(this.value)" >
               <option value="0" selected="selected">...SELECT CITY...</option> 
            <?   while($s = mysql_fetch_array($sql4))
                {
                    $selected = "";
                    $categoryId = $s['state_id'];
                    $categoryName = $s['state'];

                    if($catid == $categoryId)
                    {
                    $selected = "selected";
                    }?>
                <option value="<?php echo $s['state_id']; ?>" <?php echo $selected; ?> ><?php echo $s['state']; ?></option><?php } ?>
                </select>
                <?php //} ?>
                <input type="hidden" name="spaid" id="spaid" value="<?php echo $id; ?> />
                </td></tr>

                <tr>
                  <td colspan="2"><div align="left" id="citydiv"><ul>

<?php 
$place      = $_GET['place'];
    $state_id   = $catid; // IMPORTANT
$result     = mysql_query("select c.city, c.city_id, (CASE WHEN FIND_IN_SET(c.city_id, cp.city_id) THEN 1 ELSE 0 END) as checked from cities as c, city_place as cp where cp.spaid = {$id} and c.state_id = {$state_id}");

    while($p=mysql_fetch_array($result)) 
    {
        $city_id    = $p['city_id'];
        $city       = $p['city'];
        $checked    = $p['checked'] == 0 ? '' : " checked";
        echo "
        <li>
            <label>
            <input type='checkbox' name='place[{$city_id}]' value='{$city_id}' onfocus='myfunction()' {$checked} />
                {$p['city']}
            </label>
        </li>
        ";
    }


?>
</ul></div></td>
                </tr>

here it will show both selected and non selected checkbox values.for comparision of both city_place.city_id and cities.city_id its easy so that i stored i stored in a single field.
in frontend i want to display city values based on state_id and spaid.if i use explode it shows array.which is the better way to display the city values

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.