sylesia Posted December 4, 2006 Share Posted December 4, 2006 Ok, I know this is probably a really simple 2 part question, but for the life of me, I cannot figure this out. Pulling out a string from the database, I want to set a variable equal to part of the string, ie, if the string is HE,EM,OT I want $first = HE the comma will always be there as a marker, unless its the last two letters.The second part involves interaction with html. Using the information gained above, I want to use it so I can have an HTML box pre checked if its value was in the variable or not to be checked if it was not. Example is thisDB has EM, MO inside itpull out the information and stores CHECKED in $EMthe HTML page has a line like <TR> <TH>Emotional: <TD><input TYPE="checkbox" NAME="StrongDimensions[]" VALUE="EM">what I want is basically the line to look like this<TR> <TH>Emotional: <TD><input TYPE="checkbox" NAME="StrongDimensions[]" VALUE="EM" <? echo $EM; ?>>in other words, since EM was in the database, it stored Checked in $EM and than on the HTML page it checks the box since its trueNow, I know this is probably confusing, it is hard to really explain well what I want, but hopefully tis clear enough to make sense that someone can help me. Link to comment https://forums.phpfreaks.com/topic/29363-a-simple-parsing-question/ Share on other sites More sharing options...
Mr_Pancakes Posted December 4, 2006 Share Posted December 4, 2006 you first problem can be solved by using the function explode(); to parse your string into an array. in your case, if the string pulled from your database is "HE,EM,OT" then you could do the following:[code]// get string from database$your_db_string = "HE,EM,OT";$answer = explode(",", $your_db_string);echo $answer[0];echo $answer[1];echo $answer[2];[/code][u]results in:[/u]HEEMOTyour second problem is a bit unclear. all i could say about it from what i understand is that it would be more efficient to use a 1 or 0 in your database to represent checked or unchecked. then use a simple evaluation of the 1 or 0 to echo a checked="checked" in your html input tag. if you can better explain your second problem, it might be easier to answer.hope this helps,cheers,steve Link to comment https://forums.phpfreaks.com/topic/29363-a-simple-parsing-question/#findComment-134669 Share on other sites More sharing options...
sylesia Posted December 4, 2006 Author Share Posted December 4, 2006 Ok, more my question is in html, how would I have the box prechecked if the value is in the array. Example using yours is if $answer[0] = EMif (answer[0] == 'EM')<TH>Mental: <TD><input TYPE="checkbox" NAME="StrongDimensions[]" VALUE="ME" CHECKED>else<TH>Mental: <TD><input TYPE="checkbox" NAME="StrongDimensions[]" VALUE="ME">only I have 15 of those boxes, and I do not want to have 15 if else statements, so hoping there was a way that I could have a php variable for CHECKED that is either CHECKED or blank depending on the $answer array. Link to comment https://forums.phpfreaks.com/topic/29363-a-simple-parsing-question/#findComment-134754 Share on other sites More sharing options...
sylesia Posted December 4, 2006 Author Share Posted December 4, 2006 Also, tried what you suggested and it works when its all I do, but for some reason, when i combined it with HTML, I get a time out error. Not sure what is wrong with my code...<TR> <TD><? echo $StrongDim; $i = 0; $view = explode (",",$StrongDim); while($view[$i] != "XX"){echo $view[$i]; $i = $i + 1;}?>thats the code and it shows ME,PH,XX MEPHXX so I know that $view[$i] [i]eventually[/i] equals XX, but for some reason I get the error Fatal error: Maximum execution time of 60 seconds exceeded in http://localhost/srh/cadetyellowreview.html on line 103where line 103 is what I just showed. I tried both single and double quotes but to no end, it still waits 60 seconds and tells me there is a time out error. Link to comment https://forums.phpfreaks.com/topic/29363-a-simple-parsing-question/#findComment-134798 Share on other sites More sharing options...
Mr_Pancakes Posted December 6, 2006 Share Posted December 6, 2006 [quote author=sylesia link=topic=117259.msg478346#msg478346 date=1165231522]only I have 15 of those boxes, and I do not want to have 15 if else statements...[/quote] you could call a function to print "checked" that is defined at the top of your page (or in a different page that is referenced in this page via the inlcude() function): [code]<?phpfunction printChecked($value){ if ($value == 'EM') print "checked";}?>...<html>...<input TYPE="checkbox" NAME="StrongDimensions[]" VALUE="ME" <?php printChecked($answer[0]); ?>>...</html>[/code] [quote author=sylesia link=topic=117259.msg478391#msg478391 date=1165240943]Also, tried what you suggested and it works when its all I do, but for some reason, when i combined it with HTML, I get a time out error. Not sure what is wrong with my code... <? echo $StrongDim; $i = 0; $view = explode (",",$StrongDim); while($view[$i] != "XX"){echo $view[$i]; $i = $i + 1;}?>[/quote] it has nothing to do with the HTML, it is the PHP that is stuck in a neverending loop somewhere in your page. if i understand what you're trying to do with "ME,PH,XX" (which is just not print "XX"?), then try this instead: [code]<?phpecho "$StrongDim <br />";$view = explode (",",$StrongDim);foreach($view as $key => $val){ if ($val != "XX") echo "$val <br />";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/29363-a-simple-parsing-question/#findComment-136137 Share on other sites More sharing options...
craygo Posted December 6, 2006 Share Posted December 6, 2006 Try this out[code]<?php$your_db_string = "HE,EM,OT";$answer = explode(",", $your_db_string);// now echo out the htmlif (array_search("EM", $answer) == NULL){echo '<TH>Mental: <TD><input TYPE="checkbox" NAME="StrongDimensions[]" VALUE="EM">';} else {echo '<TH>Mental: <TD><input TYPE="checkbox" NAME="StrongDimensions[]" VALUE="EM" checked>';}?>[/code]array search will return the key if the value is found else it will return null. This way you search the entire arrayRay Link to comment https://forums.phpfreaks.com/topic/29363-a-simple-parsing-question/#findComment-136166 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.