Jump to content

Make array and loop it - need help


liquidmind

Recommended Posts

The list items aren't the same everytime so I can't put array("abc","def","efg"); into the code. The array needs to be taken from $_POST.

 

I have this so far:

 

<?
if (!isset($_POST['itemlist']))
{
?>

<form name="form" method="post">
<textarea rows=5 cols=20 name="itemlist" value="<?=$_POST['itemlist']?>" </textarea>
<input type="submit" name="formsubmit" value="Submit!" class="button_style">

</form>

<?
}
else {

$itemlist = explode("\n",$_POST['itemlist']);
$count = 0;
foreach ($itemlist AS $item) {

its works pretty as well for me... :(

 

else {

 

$itemlist = explode("\n",$_POST['itemlist']);

$count = 0;

foreach ($itemlist AS $item) {

echo $item;

if (preg_match('/eggs/',$item )) {

    echo $value."A match was found.</br>";

} else {

    echo $value."A match was not found.</br>";

}

 

So I don't need to do $var = array() anywhere ?

 

Right now what it is doing is giving me the preg_match results for the last item in the list only. If I have one item in the list it will give me the results for that one item. If the list is two items then it will give me the results for item 2 while showing null results for 1. If the list is three items, it will give me the results for item 3 while showing null results for 1 and 2...etc.

 

Where it says "xxxx" it's a filler to protect the innocent.  :)

 

<?
if (!isset($_POST['itemlist'])) 
{
?>

<form name="form" method="post">
<textarea rows=5 cols=20 name="itemlist" value="<?=$_POST['itemlist']?>" </textarea>
<input type="submit" name="formsubmit" value="Submit!" class="button_style"><br />
</form>

<?
} 

else {


$itemlist = explode("\n",$_POST['itemlist']);
$count = 0;
foreach ($itemlist AS $item) {

$ab=curl_init();

$link="xxxx"

curl_setopt($ab,CURLOPT_URL,$link);
curl_setopt($ab, CURLOPT_HEADER, 0);

ob_start();
curl_exec($ab);
curl_close($ab);
$item_results=ob_get_contents();
ob_end_clean();

preg_match (xxxx)

$count_matches=$match[1];

if($count_matches=="")
$count_match=0;

echo "<br/>".$item." ".$count_match."";

$count++;
}
}
?>

try this........

 

$link="/xxxx/";

if(preg_match ($link,$item))

{

 

 

$count_matches=$match[1];

 

if($count_matches=="")

$count_match=0;

 

echo "found".$item." ".$count_match."<br/> ";

}else

{echo "notfound".$item." ".$count_match."<br/> ";

}

$count++;

}

}

count_matches is the found or not found. If it returns matches then found, if not, then not found. No need to add echo "found" or "not found". The count_matches=0 is the "not found".  With that in mind there is no point to doing an if statement at preg_match.

 

Right now what it is doing is giving me the correct preg_matches results for the last item in the list only. If I have one item in the list it will give me the results for that one item. If the list is two items then it will give me the results for item 2 while showing null results for 1. If the list is three items, it will give me the results for item 3 while showing null results for 1 and 2...etc.

 

echo "<br/>".$item." ".$count_match."";

Should be

echo "<br/>".$item." ".$count_matches."";

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.