Reputation: 321
I am working on my PHP to check to output the number in the variable that if the string is contains then do something.
When I try this:
$attached_file = 'attid: ' . $attid . ' filename: ';
if(strpos($files, $attached_file') !== false) {
echo "here.......................now";
}
It wont let me get pass so when I try this:
$attached_file = 'attid: 0 filename: ';
if(strpos($files, $attached_file') !== false) {
echo "here.......................now";
}
It works fine. I can get pass on the if statement with no problem.
Here is the array for the $attached_arr
:
Array ( [0] => attid: 0.1 filename: what-is-bootstrap.png [1] => attid: 0 filename: how-ajax-work.png [2] => )
Here is the full code:
<?php
//Connect to the database
include('config.php');
$id = '3';
$mailbox = $link->prepare("SELECT * FROM inbox WHERE id = ?");
$mailbox->execute([$id]);
$attached_arr = array();
// set the resulting array to associative
$row = $mailbox->fetch(PDO::FETCH_ASSOC);
if($mailbox->rowCount() == 1)
{
$mailbox_attached_files = $row['attached_files'];
$attached_arr = explode("\n", $mailbox_attached_files);
$attid = 0;
foreach ($attached_arr as $files) {
$attached_file = 'attid: ' . $attid . ' filename: ';
$attached = '';
if(strpos($files, $attached_file) !== false) {
echo "here.......................now";
//$attached = trim(strrchr($files, ':'), ': ');
}
$attid++;
}
}
$mailbox = null;
?>
Can you please show me an example how I could pass on the if statement when I output the number in the variable attached_file
??
Thank you.
Upvotes: 2
Views: 88
Reputation: 3507
Ok, The problem you get is that you increment $attid
by hall number = 1 on each loop thru array. Like an example of your array, occurrence of 0
shoud be on second iteration, but $attid
will be already equal to 1. Here is my vision:
$attached_arr = array (
"attid: 0.1 filename: what-is-bootstrap.png",
"attid: 0 filename: how-ajax-work.png"
);
$attid = 0; //initial id
//
for($i = $attid; $i < 0.5; $i += 0.1){
//run thru the array
foreach ($attached_arr as $files) {
$attached_file = "attid: " . $i . " filename: ";
echo $attached_file . "\n";
$attached = '';
if(strpos($files, $attached_file) !== false) {
echo "here.......................now\n";
}
}
}
Output
attid: 0 filename: //0.1
attid: 0 filename: //0
here.......................now//we get output
//second increment now $attid = 0.1
attid: 0.1 filename: //0.1
here.......................now // we get output
attid: 0.1 filename: //0
Hope it Helps.
Upvotes: 1