Jump to content

how to explode info stored in database to fill out a form.


jbrill

Recommended Posts

hey guys,

im trying to get some information from my database, and then check off the checkboxes if they do infact exist in the particular row.

 

i have the following code to query the database, and display the checkboxes, this is working.

<div align="center" style="width:250px;border:1px solid #666;height:100px;overflow:scroll;text-align:left;">
		<?
		$colorsquery = mysql_query("SELECT * FROM colors");
		while ($colors = mysql_fetch_array($colorsquery))
		{
		echo '<input type="checkbox" name="color[]" value="' . $colors['id'] . '"">' . $colors['name'] . '<br />';
		}
		?>
		</div>

 

Now i need to  use the following query to get information form the "colors" row in my database and check off if the appropriate box if it does indeed contain the right data.

 

the trick is, the row "colors" holds the id of the selected colors by semi colon and i do not know how to pull it all apart. it is my first time using the "explode" function.

 

the stored color information looks like this: 1;2;3;4;5;6;7 and so on

 

 

$array_name = explode(';',$string_from_database);

 

 

$array_name is the new array name you want

 

explode is the function

 

';' is defining where one array element ends and next begins

 

$string_from_database is the string you want to break up

 

you can then asess the array by

$total_array_elements = count ($array_name);

for ($n=0; $n < $total_array_elements; $n++) {

//Do something here with your data

}

 

er hope that helps

$string_from_database="1;2;3;4;5;6;7";

 

$array_your_making= explode(';',$string_from_database);

 

$count_the_array_values=count($array_your_making);

 

for ($n=0; $n < $count_the_array_values; $n++) {

 

echo $array_your_making[$n] . "<br>";

 

}

 

part one is the string you got from your database

part two you split the data up

part three you count the values of the array

part four you make a loop until the end of the array

part five you send to the page the values of the array

 

this should output

 

1

2

3

4

5

6

7

 

Thus breaking a string into an array ... ?

<div align="center" style="width:250px;border:1px solid #666;height:100px;overflow:scroll;text-align:left;">
		<?
		$colorsquery = mysql_query("SELECT * FROM colors");
		while ($colors = mysql_fetch_array($colorsquery))
		{
			$var = explode(';',$colors['color']);
			if(count($var)>0){
				foreach($var as $val_id){
				echo '<input type="checkbox" name="color[]" value="' .$val_id. '"">' . $colors['name'] . '<br />';
				}
			}
		}//note i dont think you need the while loop here
		?>
		</div>

@Distant_storm i guess  correct  any ways try that

 

here is what i have, right now im just trying to display the strong without the semi colon.

<? 
		$array = $modify['colors'];
		foreach($array as $curcolor) 
		{
			 $curcolors = explode(';',$curcolor);
			 echo $curcolors;	
		}

		 ?>

 

this doesnt appear to work, i have probably done something wrong

 

explode returns an array so echoing the result from explode will give you this array()

 

get the specific index of that array

the syntax should be

 

$var = explode (';','value here');

foreach($var as $val) {

echo $val;

}

 

 

ok, i think i might be getting close.

here is my code now:

<div align="center" style="width:250px;border:1px solid #666;height:100px;overflow:scroll;text-align:left;">
		<?
		// get the sperated color id values from the products table, row "colors"
		$var = explode (';',$modify['colors']);
			foreach($var as $val) 
			{
			$checkedcolor = $val;
			}

		//select all colors from the "colors" table
		$colorsquery = mysql_query("SELECT * FROM colors");
		while ($colors = mysql_fetch_array($colorsquery))
		{
			echo '<input type="checkbox" name="color" value="' . $colors['id'] . '">' . $colors['name'] . '<br />';
		}

 

now i need an if statement (i think) so that id the seperated color id's are equal to any of the id's from the colors cable, they will be echo "checked" in the input field

 

how would i go about doing this?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.