Jump to content

A simple parsing question


sylesia

Recommended Posts

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 this

DB has EM, MO inside it
pull out the information and stores CHECKED in $EM
the 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 true

Now, 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
Share on other sites

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]
HE
EM
OT

your 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
Share on other sites

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] = EM
if (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
Share on other sites

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 103
where 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
Share on other sites

[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]
<?php
function 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]
<?php
echo "$StrongDim <br />";
$view = explode (",",$StrongDim);
foreach($view as $key => $val)
{
    if ($val != "XX") echo "$val <br />";
}
?>
[/code]
Link to comment
Share on other sites

Try this out

[code]<?php
$your_db_string = "HE,EM,OT";
$answer = explode(",", $your_db_string);
// now echo out the html
if (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 array

Ray

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.