dealing with array : form, php and mysql
kigoobe
Status: New User - Welcome
Joined: 01 Sep 2005
Posts: 1
Reply Quote
Hi Guys

This is my first post in the forum ... I've just found it ...

Well, so, In my form, I have used something like this.

[php]
for ($i=1;$i<=$rno;$i++) {
print '<tr><td align="right" valign="top"><input type="text" name="room_type'.$i.'"></td>
<td align="left" valign="top"><textarea cols="25" rows="6" name="room_descr'.$i.'"></textarea></td></tr>'; }
[/php]

We can see that the inbox and the associated textarea will appear depepdning on the value of $rno ... thus, 3, 4, 5 or whatever times.

In a normal case, I could have treated them (had it been an isolated case of an inbox and a textarea) as follows:

[php]
$room_type = htmlspecialchars($_POST['room_type']);
$room_descr = htmlspecialchars($_POST['room_descr']);
$room_descr = addslashes($room_descr);
$room_descr = ereg_replace("\r",'',$room_descr);
$room_descr = ereg_replace("\n\n",'</p><p>',$room_descr);
$room_descr = ereg_replace("\n",'<br />',$room_descr);

$descr = "INSERT INTO pho_rooms SET
room_type='$room_type',
room_description='$room_descr',
hotel_room_id='$id'";
[/php]

And could have extracted the data from the database once inserted with something like this:

[php]
$cid=mysql_insert_id();
$hotel_details=@mysql_query("SELECT hotel_name, room_type, room_description FROM pho_hotel, pho_rooms WHERE pho_hotel.id=hotel_room_id AND room_id='$cid'");
if (!$hotel_details) { die('<p>Error retrieving data from the database. Error: '. mysql_error() .'</p>'); }
while ($abc=mysql_fetch_array($hotel_details)) {
$hotel_name=stripslashes($abc['hotel_name']);
$room_type=stripslashes($abc['room_type']);
$room_descr=stripslashes($abc['room_description']);
}
[/php]

I was wondering, since here it a sort of an array, how should I proceed to do the same ?

Any idea friends ?
Cheers.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4124
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
Without getting into the specifics of your code, which I can't see how would work off the top of my head, when I want to insert an array into mysql, I just loop through the array.

I know that's not what you're asking, but it's kind of hard to get a real sense of it. If I was going to write out a looped form like you're doing, I'd create a field name made out of two features, one the actual db field, and the other the id of that field, like name="field-12" or something, there's several ways to do it. Normally I'd use the value as the id I think, but since it's a textarea, that wouldn't work the way you want it to.

You can also use a different php syntax, where you insert an [ ] after the field name, that tells php that the form fields are an array, not singular, like so: name="field[ ]", that works for checkboxes, not sure about text areas, I think it might.

Then I'd split the form field name to extract the proper name, 'field' and the id number, '12'.

To loop through the fields for insertion, you just do sort of the reverse of what you had thought of for writing it out, in other words:

:: Code ::
for ( $i=0; $i < something; $i++ )
{
if ( if ( array_key_exists( 'field-' . $i, $_POST ) )
  insert into db....
}

Or something like that. This stuff is hard to really explain without seeing an actual working code sample though.

by the way, no promotional/spam/seo sigs please.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours