PDA

View Full Version : PHP Arrays & If Statements



shnawer
10-11-2009, 10:21 PM
hello

I am new to php and i am trying to split a statement and trying to validate it but its giving me T_IF error. Can i call "if" to validate Arrays?

My script is following;



$__strText = "love,is,life/noew,0,<a href,o";
$__chunk = explode(",",$__strText);



foreach ($__chunk as $html) {
$counter = $counter +1
If ($html == "<a href")
{
echo "$counter found";
}
}



Thanks for your help

bluewalrus
10-12-2009, 12:46 AM
Not sure what your trying to do or what the original sting is but I think this would work


$__strText = "love,is,life/noew,0,<a href,o";
$__chunk = explode(",",$__strText);



foreach ($__chunk as $html) {
if ($html[$counter] == "<a href")
{
echo "$counter found";
}
$counter = $counter +1
}

djr33
10-12-2009, 01:46 AM
bluewalrus, $html[$counter] is treating $html as an array-- foreach is taking each array item (in this case strings) out of the array, so now you don't need to use a counter. You could use a while() loop instead and THEN use a counter with $__chunk, but not as is.

I'm not sure what "$counter" is used for, but if you want to count the number of instances the string appears in the array, this would work:


$__strText = "love,is,life/noew,0,<a href,o";
$__chunk = explode(",",$__strText);

$counter=1;
foreach ($__chunk as $html) {
if ($html == "<a href")
{
$counter = $counter +1;
}
}
echo "$counter found";

The T_IF error is generated in the original code because you forgot the semicolon at the end of the +1 line. The order of what you were doing didn't make much sense to me, so I moved it around. If you want the original order just add the semicolon to the end of the line. "If" should not be capitalized. It appears that it doesn't matter in this case, but lowercase/uppercase letters do matter some of the time so you should get into the habit of always using lowercase unless there's a reason not to.


Also, you can just use $counter++; as a shortcut to increase it by one.


And finally, you could probably use another function actually designed to count and skip a step or two here. But anyway, the code above will work.

bluewalrus
10-12-2009, 05:22 AM
yea i completely misread this whole thing i think or my head was somewhere else.. either way my comment should be disregarded and not used.

shnawer
10-12-2009, 10:55 PM
thank you guys i am so used of vbscript thats why i use upercase and lowercase there is another question if anyone kindly reply

I want to display picture database 5 pictures in row how i will add "</tr><tr>" after 5 records in vbscript we used Mod of Counter but in php i dont know can someone explain me?

djr33
10-13-2009, 12:44 AM
You can try this with foreach().

echo "<table><tr>";
$tdcount=0
foreach(......)
{
echo $img;
$tdcount++;
if ($tdcount==5)
{
$tdcount = 0;
echo "</tr><tr>";
}
}
echo "</tr></table>";


That is the basic approach. Personally I don't like using foreach() loops unless the operations are generally simple (this is not, really). So I would use foreach(), etc. to generate an array with all of the images in it, then use a while() or for() loop to generate the table. But if you aren't getting much more complex than this, foreach() should work enough.