DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Need help with combo

By: rekha singh | 13 Mar 2010 12:57 pm

I am trying to figure out how to get two columns into one combo. I have two columns. One column for first name and other columns for last name. I want first and last name in one combo. Here is my codes. Can someone tell me what went wrong?

<?php
include 'dbc.php';

echo'<select name="categories" >';
$res=mysql_query( "select * from missionary") ;
if(mysql_num_ rows($res) ==0) echo "there is no data in table..";
else
for($i=0;$i< mysql_num_ rows($res) ;$i++) {
$row=mysql_fetch_ assoc($res) ;

$both[full_name] = ($row[first_ name]) + ' ' + ($row[last_name] );

echo"<option> $both[full_ name]</option> ";

}
echo'</select> ';

?>

 

 


 

Comments

Is your code erroring out? You can do it way simpler:

foreach($query as $row){
echo "<option>$firstname , $lastname</option> "
}

Or whatever you need to do in the foreach, but the point is, if you have an array, foreach is the best way to go

By: rekha singh | 13 Mar 2010

It would be helpful if you said what sort of error/problem you had, but I think you need quotes around the parameters in $row, so:

$both['full_ name'] = ($row['first_ name']) + ' ' + ($row['last_ name']);

otherwise they are just variables which have unset values.

By: rekha singh | 13 Mar 2010

I tried that and it did not work. There is no error msg but when I look at combo and click on it. It has many 0's and there is no first or last name.

If I change back to old code like
// $both['full_ name'] = ($row['first_ name']) + ' ' + ($row['last_ name']);
// echo"<option> $both[full_ name]</option> ";
echo"<option> $row[first_ name]</option> ";

This one is working only for first name. I am trying to add the last name on it. Because some of them have same first name. I want to add last name so I can select the right person.

By: rekha singh | 13 Mar 2010

Pl. try like this:

<?php
include 'dbc.php';

echo '<select name="categories" >';
$res=mysql_ query("select * from missionary") ;
if(mysql_num_ rows($res) ==0) echo "there is no data in table..";
else
while($row=mysql_ fetch_array( $res))
{
    echo "<option>".$ row['first_ name']." ".$row['last_ name']."< /option>" ;
}
echo'</select> ';

?>

By: rekha singh | 13 Mar 2010

Well, at first glance it looks like you are using the wrong operator for concatenation.

$both[full_name] = ($row[first_ name]) + ' ' + ($row[last_name] );

Should be:
$both[full_name] = ($row[first_ name]) . ' ' . ($row[last_name] );

In PHP the . is the concat operator, not the +

Hope this helps,

By: rekha singh | 13 Mar 2010

Thank you and now it is working. Next thing I need to get more information from table into fill out text box. I do not know much about combo. When we select the item from combo, can it bring inforamation from table into text boes? Or do we need a button to get information from tables into text boxes? I am create a form to get information from table into different text box for update if needs. I will use UPDATE SET. I am still learning how to do that. I do know how to update on mysql but not on PHP.

By: rekha singh | 13 Mar 2010

With a little javacript goodness, you can chain select boxes. Essentially when the first 'select' is chosen, you can have an ajax call to your php script, have it grab the contents for the next box, and inject it. With jQuery this can be accomplished with very little code. There should be some great examples online.

For using SQL via PHP, a good start would be something like:

$tbl = 'foo';
$id = 4;
$sql = "SELECT * FROM {$tbl} WHERE id = {$id}";
$res - mysql_query( $sql);

And some reading at php.net/mysql wont hurt.

To update, it would be:

UPDATE `table` SET `field`=value WHERE `field` = 'value';

Hope this helps,

By: rekha singh | 13 Mar 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move