Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display certain contents of an array, selected by checkbox

Status
Not open for further replies.

kusarigama

Technical User
Apr 24, 2002
45
DE
Hi all,

I have a textfile with email addreses. With the following script, I try to display only the selected emails (Checkbox)
It won't work. Can anybody help me ?

Thanx in advance

Olli


$fp = fopen($reader,"r");
while(!feof($fp))
{
$emails = chop(fgets($fp,260));
if($emails == "")
{
continue;
}

for($i=0; $i<count($emails);$i++)
{
?>
<input type=&quot;checkbox&quot; name=emails[] value=<?echo $i;?>><?echo $emails;?><br>
<?
if(isset($email_sel)) // submit button
{
print &quot;<br>&quot;.$i;
}
}



}
fclose($fp);
 
Define &quot;doesn't work&quot;.
______________________________________________________________________
TANSTAAFL!
 
Unless you tell us what doesn't work means in your case, we won't be able to help you. //Daniel
 
Ok - sorry ;)

My script (see above)

I have a Textfile with email addresses.
For every content of the textfile(emails), a checkbox will be created. When I execute the script and I make a selection - I want the script to output the selected contents ... This doesn't work.
An example would be fine. I know that's just a little bug, but this bug makes me bite in my desk ;)

Thanx in advance
Olli
 
I'm not sure I understand what you want, but if I understand it correctly, you want to submit some checkboxes to a script that outputs the selections. Is this what you want?
If so, you could use this script to output the selections:
Code:
<?php
$fp = fopen($reader,&quot;r&quot;);           
for($i = 0; !feof($fp); $i++)
{
    $emails = chop(fgets($fp,260));
    if(empty($emails))
        continue;
    if (in_array($_POST['emails'], $i))
        echo $emails;
}
fclose($fp);
?>
//Daniel
 
First: THANX a lot =)

I want to have the selected emails to be send via mail. The form is using &quot;$php_self&quot;.

one question: That what I wrote in the checkboxe's expression is ok, right ? And I can put it in your for() loop.

 
Here is a whole script that submits to itself.
Code:
<?php
$fp = fopen($reader,&quot;r&quot;);
if (isset($_POST['submit']))
{
    for($i = 0; !feof($fp); $i++)
    {
        $emails = chop(fgets($fp,260));
        if(empty($emails))
            continue;
        if (in_array($_POST['emails'], $i))
            echo $emails;
    }
}
else
{
?>
<form action=&quot;<?=$_SERVER['PHP_SELF']?>&quot; method=&quot;POST&quot;>
<?php
    $i = 0;
    while (!feof($fp))
    {
        $email = chop(fgets($fp,260));
        if(empty($email))
            continue;
        echo '<input type=&quot;checkbox&quot; name=&quot;emails[]&quot; value=&quot;' . $i . '&quot;>' . $emails . '<br>';
        $i++;
    }
?>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; />
</form>
<?php
}
fclose($fp);
?>
I think this should do what you want. //Daniel
 
Thnax again ;)

Works good - now i need to display the selected emails.

This won't work with print $emails[$i].

Just the last thing, and I'm satisfied ..

Olli
 
Whoops, change
Code:
        if (in_array($_POST['emails'], $i))
to
Code:
        if (in_array($i, $_POST['emails']))
This already outputs the selected emails when you submit. It doesn't store the emails in an array, it reads from the file, and if the line number was one of the ones submitted, it displays it. //Daniel
 
hehe - I understand your script ;)

You use $_POST. Is it possible via a function() too (without using $_POST ?
I tried to store the $emails[$i] in a function. But guess: There is also an error somewhere ... sorry ;)

The script is :

fp = fopen($reader,&quot;r&quot;);

function rt($damails)
{
global $fp;

for($i = 0; feof($fp); $i++)
{
$emails = chop(fgets($fp,260));
if(empty($emails))
{
continue;
}

if (in_array($i, $damails))
{
echo $emails;
}
}
}

$i=0;
while(!feof($fp))
{
$emails = chop(fgets($fp,260));
if(empty($emails))
{
continue;
}
print'<input type=&quot;checkbox&quot; name=&quot;emails[]&quot; value=&quot;'.$i.'&quot;>'.$emails.'<br>';

$i++;

}
if(isset($_POST['email_sel']))
{
rt($emails);
}
//fclose($fp);
 
I don't think you understand what you are doing.
Your $emails variable only stores a string containing the last line read from the file. Also, your for loop is wrong as it tests for feof($fp), which should read !feof($fp).
$_POST is the auto-global array containing the variables sent to the script using the POST method. If you have register_globals off, you have to use it to access the POST variables. $_GET is used for GET variables. //Daniel
 
Ohh that was my mistake. I deleted that &quot;!&quot; by mistake.
 
There is still an error message regarding the is_array() command. Wehen it tries to compare the emails, it fails

But everything looks good so far. Register:globals is on

Olli
 
Uhh -forgo teh code, here it is ...

$fp = fopen($reader,&quot;r&quot;);

if(isset($_POST['email_sel']))
{
for($i=0; !feof($fp); $i++)
{

$emails = chop(fgets($fp,260));
if(empty($emails))
{
continue;
}
if (in_array($i,$_POST['emails']))
{
echo $emails;
}
}
}


$i=0;
while(!feof($fp))
{
$emails = chop(fgets($fp,260));
if(empty($emails))
{
continue;
}
print'<input type=&quot;checkbox&quot; name=&quot;semails[]&quot; value=&quot;'.$i.'&quot;>'.$emails.'<br>';
$i++;
}
fclose($fp);
 
By the way i corrected the error with semail[]. I'v put $emails in and it's still not working ...

print'<input type=&quot;checkbox&quot; name=&quot;semails[]&quot; value=&quot;'.$i.'&quot;>'.$emails.'<br>';
 
I don't see what you are trying to accomplish. What is it that you actually want to do? //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top