Jump to content

explode values


megz90

Recommended Posts

hey, ive managed to get working a while loop which displays a check box for each record in a table.

the user can select a number of check boxes which then implode into a string for storage in a database

 

implode...

$glued = implode(',', $_REQUEST['interest']);

if (isset($_REQUEST['interest']))
{
  echo implode(',', $_REQUEST['interest']);
}

ie if check box for serviceID 1 2 4 and 5 from the loop at the top were ticked then the string would read 1,2,4,5

 

so this adds in the database ok. when i take the value out of the database i can use explode to get the array back out on the screen

$serv = "{$row['dbservices']}";
print_r (explode(",",$serv));

the above prints out this : (i had service 1 2 4 and 5 selected from the check boxex)

output=

Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 )

 

what i want to do is find out what the values from the out put mean. ie

for each value in the array look at the service table and check for the servicename where the serviceID is the same as each of the array items.

is that somehting the php can do, how would i go about trying to do that ?

thanks for any help

 

Link to comment
Share on other sites

make booking . php

[code=php:0]<?php 
// admin page
include('../db_connection.php');
if (!isset($_SESSION["sess_loggedon"])) {
        session_start();
        } 
	else header ('location: index.html');
$query  = ("SELECT * FROM xhorse WHERE dbOwnerId='".$_SESSION['sess_loggedon']."'");
$result = mysql_query($query);
$queryser  = ("SELECT * FROM xservice");
$resultser = mysql_query($queryser);
?>

[/code]

...other half with the form

[code=php:0]<form action="bookadd.php" method="POST">
<?php 
echo "Select Your Horse :";
echo "<select name='selecth' id='selecth'>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<option value=\"{$row['dbhorseid']}\" >{$row['dbhorsename']}</option>";
}
echo "</select>";
echo "<br/>";
echo "Date : <input name=\"date\" type=\"text\" size=\"10\" maxlength=\"10\" />";
echo "<br/>";
?>
<table border="1">
<tr><td>Tick if needed</td><td>service name</td><td>cost</td><td>description</td></tr>
<?php
while($row = mysql_fetch_array($resultser, MYSQL_ASSOC))
{ 
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"interest[]\" value=\"{$row['dbserviceId']}\"/></td>";
echo "<td>{$row['dbservicename']}</td>";
echo "<td>£{$row['dbcost']}</td>";
echo "<td>{$row['dbdescription']}</td>";
echo "</tr>";
echo "<br/>";
}
echo "date: ";



?>
</table>
<input name="submit" type="Submit" value="Confirm Booking" />
</form>

[/code]

 

i haven't included the bit where it glues it together or puts it in the DB but if u would like to see it, say..

Link to comment
Share on other sites

for your check boxes redo them to be in the form

<?php
echo "<td><input type=\"checkbox\" name=\"interest[".$row['dbserviceID']."]\" value=\"1\"/></td>";
?>

and then you can get the ones checked via doing

<?php
foreach($_POST['interest'] as $key => $value){
if($value == "1"){
#Its checked
#the serviceID is $key
}
}
?>

 

Link to comment
Share on other sites

hmm. im not sure if i have misunderstood but

each and every checkbox will have the value of 1 from what im reading,

 

value=\"1\"

it'll show all the checkboxes on the screen but they are all one in value when it gets added to the database

 

what would u suggest?

would u care to look at the whole of my code?

Link to comment
Share on other sites

yeah the services names are in a table called

xservices containing  (serviceid, sname, cost, description,)

in the booking table i have a field called 'services' which should store all the services that a cust selects and i thought the implode all the selected serviceId would be the best way.

 

so if a cust wants to edit a booking i can just show a edit form with the things they selected and all the other services as well so they can choose.

 

iwas looking at doing this  in a different way to start with.

http://www.phpfreaks.com/forums/index.php/topic,188079.msg843084.html#msg843084

 

but was told not to go that way as it would be very difficult.

 

what do you think ?

 

Link to comment
Share on other sites

try redoing your query to be a bit better

since I don't know your exact table structure I can't be too specific but try something like

<?php
$q = "Select xhorse.dbhorseid as horseid, xhorse.dbhorsename as horsename, 
xservice.dbserviceID as ServiceID, xserviec.dbservicename as servicename, xserviec.dbdescription as dbdescription from `xhorse`, `xservice` 
where xhorse.dbOwnerID = '".$_SESSION['sess_loggedon']."'" 
Group By xhorse.dbhorseID";
$r = mysql_query($q) or die(mysql_error()."<br /><br />."$q);
while($row = mysql_fetch_assoc($r)){
print_r($row);
echo "\n\n\n\n\n";
}
?>

See how that works.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.