jbrill Posted December 4, 2007 Share Posted December 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted December 5, 2007 Share Posted December 5, 2007 $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 Quote Link to comment Share on other sites More sharing options...
jbrill Posted December 5, 2007 Author Share Posted December 5, 2007 still pretty confused, any more suggestions? Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted December 5, 2007 Share Posted December 5, 2007 $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 ... ? Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 5, 2007 Share Posted December 5, 2007 <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 Quote Link to comment Share on other sites More sharing options...
jbrill Posted December 5, 2007 Author Share Posted December 5, 2007 hmmm no luck yet. any more suggestions? Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 5, 2007 Share Posted December 5, 2007 show the stored data in your DB and how to you want to call them! Quote Link to comment Share on other sites More sharing options...
trq Posted December 5, 2007 Share Posted December 5, 2007 I suggest you find a tutorial on database normalization. Stroring a semi colon sperated string is a pretty poor design chioce. Theres a link in my sig to a free book that has a chapter on the subject. Quote Link to comment Share on other sites More sharing options...
jbrill Posted December 5, 2007 Author Share Posted December 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
jbrill Posted December 5, 2007 Author Share Posted December 5, 2007 any ideas? Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 5, 2007 Share Posted December 5, 2007 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; } Quote Link to comment Share on other sites More sharing options...
jbrill Posted December 5, 2007 Author Share Posted December 5, 2007 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? Quote Link to comment Share on other sites More sharing options...
jbrill Posted December 5, 2007 Author Share Posted December 5, 2007 anyone? Quote Link to comment Share on other sites More sharing options...
jbrill Posted December 6, 2007 Author Share Posted December 6, 2007 .... bump Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.