Jump to content

How to fetch the filename on split


mark107

Recommended Posts

Hi all,

I am working on the PHP to fetch the data from the database. I would like to fetch the filename only following by:

email1.png
email2.png
email3.png
my_inbox.png

 

When I try this:           

$mailbox_sql = 'SELECT * FROM ' . $mailfolder . ' WHERE email_id = ? AND message_id = ?';
$mailbox = $link->prepare($mailbox_sql);
$mailbox->execute([$id,$message_id]); 

// set the resulting array to associative
$row = $mailbox->fetch(PDO::FETCH_ASSOC);

if (is_array($row)) {
   $attached = $row['attached_files'];
   $attached_files_name = explode('attid: ', $attached);
}

 

 

I am getting this:

0 filename: email1.png
1 filename: email2.png
2 filename: email3.png
3 filename: my_inbox.png

 

 

Here is what I have stored in the database:

 attid: 0 filename: email1.png
 attid: 1 filename: email2.png
 attid: 2 filename: email3.png
 attid: 3 filename: my_inbox.png


 

 

Here is what I want to achieve:

email1.png
email2.png
email3.png
my_inbox.png

 

 

Can you please show me example how I could get the filename when I am fetching the data from the database?

Thanks in advance.

Edited by mark107
Link to comment
Share on other sites

6 minutes ago, Barand said:

try


SELECT TRIM(SUBSTRING_INDEX(attached_files, ':', -1)) as filename
FROM ...

 

Why do I need to do that as I have already fetching the data and store in the array?

I want to split the string for value like "0 filename:  ", "1 filename: ", "2 filename:  ", "3 filename: " or whatever it is to replace with empty string.

Edited by mark107
Link to comment
Share on other sites

2 minutes ago, mark107 said:

Why do I need to do that

Because that is what you asked for ...

 

47 minutes ago, mark107 said:

Here is what I have stored in the database:


 attid: 0 filename: email1.png
 attid: 1 filename: email2.png
 attid: 2 filename: email3.png
 attid: 3 filename: my_inbox.png


 

 

Here is what I want to achieve:


email1.png
email2.png
email3.png
my_inbox.png

 

 

Can you please show me example how I could get the filename when I am fetching the data from the database?

 

Link to comment
Share on other sites

4 minutes ago, Barand said:

Because that is what you asked for ...

 

 

It doesn't make sense.

I have asked how to remove the string and get the filename.

I have tried this and it doesn't work.

foreach ($attached_files_name as $filename)
{
     if(strpos($filename, 'attid: ' . $attach_id) !== false)
     {
          $filename = str_replace('attid: ' . $attach_id, '', $filename);
      }
                    
      if(strpos($filename, ' attid: ' . $attach_id . ' ') !== false)
      {
              $filename = str_replace(' attid: ' . $attach_id . ' ', '', $filename);
       }

       echo $filename;
       echo "<Br>";
}

 

Link to comment
Share on other sites

Get the query to do the work.

mysql> SELECT attached_files
    ->      , TRIM(SUBSTRING_INDEX(attached_files, ':', -1)) as filename
    -> FROM mark107;
+---------------------------------+--------------+
| attached_files                  | filename     |
+---------------------------------+--------------+
| attid: 0 filename: email1.png   | email1.png   |
| attid: 1 filename: email2.png   | email2.png   |
| attid: 2 filename: email3.png   | email3.png   |
| attid: 3 filename: my_inbox.png | my_inbox.png |
+---------------------------------+--------------+
Link to comment
Share on other sites

6 minutes ago, Barand said:

Get the query to do the work.


mysql> SELECT attached_files
    ->      , TRIM(SUBSTRING_INDEX(attached_files, ':', -1)) as filename
    -> FROM mark107;
+---------------------------------+--------------+
| attached_files                  | filename     |
+---------------------------------+--------------+
| attid: 0 filename: email1.png   | email1.png   |
| attid: 1 filename: email2.png   | email2.png   |
| attid: 2 filename: email3.png   | email3.png   |
| attid: 3 filename: my_inbox.png | my_inbox.png |
+---------------------------------+--------------+

I dont store these strings seperate. I stored altogether.

See this for yourself !

database.png

Link to comment
Share on other sites

9 hours ago, mark107 said:

I dont store these strings seperate. I stored altogether.

Then you deserve to have problems :)

Try

$attached_files = <<<DATA
attid: 0 filename: email1.png
attid: 1 filename: email2.png
attid: 2 filename: email3.png
attid: 3 filename: my_inbox.png
DATA;
 
$afArray = explode("\n", "$attached_files");
 
foreach ($afArray as $af) {
    echo trim(strrchr($af, ':'), ': ') . '<br>';
}

 

Link to comment
Share on other sites

5 hours ago, Barand said:

Then you deserve to have problems :)

Try


$attached_files = <<<DATA
attid: 0 filename: email1.png
attid: 1 filename: email2.png
attid: 2 filename: email3.png
attid: 3 filename: my_inbox.png
DATA;
 
$afArray = explode("\n", "$attached_files");
 
foreach ($afArray as $af) {
    echo trim(strrchr($af, ':'), ': ') . '<br>';
}

 

Thank you very much for this as it is working great. 

Problem are now resolved.

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.