Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP inserting parsed values from text file into MySQL DB
    primarykey
    data
    text
    <p>I'm new to PHP and am trying to parse certain text from a txt file, and then insert the text into a MySQL database. So, let's get more specific. The file's format is as such, and it is repeated through the document end. the ellipses represent the previous and next tones.</p> <pre><code>... [Tone27] Atone = 707.3 Btone = 746.8 Btonelength = 3 Btonedebounce = 1 Description = Fire Department 1 mp3_Emails = email@address.com,email2@address.com,email3@address.com amr_Emails = email2@textmessaging.com,email1@textmessaging.com alert_command = c:\test.bat post_email_command = c:\test2.bat radio_frequency = 154.475 exclude_from = 13:25 exclude_to = 13:35 exclude_emails = email2@textmessaging.com,email2@address.com ... </code></pre> <p>What I want to do is parse the first items(e.g. '[tone27]') in each "tone block" from the file and insert it into the first field of a NEW row in the db. I then need to evaluate what comes before each line's " = ", for instance "Atone," and insert what comes after that line's " = ", for instance "707.3" into a field by that name. so, this row may look like this in the db:</p> <pre><code>$id | [tone27] | 707.3 |746.8 | 3 | 1 | Fire Department 1 |email1@x.com,email2@x.com,e...|... </code></pre> <p>and so on...</p> <p>i've been able to isolate each thing by performing string functions, but am unsure of how to set up a loop that would insert each value properly. Here's the code I used to isolate them, but it's not helping at all with actually getting them into the database.</p> <pre><code>$txt_file = file_get_contents('config/tones.txt'); $rows = explode("\n", $txt_file); foreach($rows as $row =&gt; $data) { $row_data = explode(' = ', $data); if ((isset($row_data[0])) &amp;&amp; ($row_data[0] !== " " )){ $info[$row]['attribute'] = $row_data[0]; $info_attribute = trim($info[$row]['attribute']); } if (isset($row_data[1])){ $info[$row]['value'] = $row_data[1]; $info_value = trim($info[$row]['value']); //display data echo 'Row ' . $row . ' Attribute: ' . $info_attribute . '&lt;br /&gt;'; echo 'Row ' . $row . ' Value: ' . $info_value . '&lt;br /&gt;'; } elseif (($info[$row]['attribute']) &amp;&amp; (!empty($info_attribute))) { echo "&lt;br&gt;"; echo 'Row ' . $row . ' Attribute: ' . $info_attribute . '&lt;br /&gt;'; continue; } </code></pre> <p>I'M A NOOB, NO DOUBT. I'M LOST. Thanks in advance for your help!!!</p> <pre><code>****|| EDIT ||**** </code></pre> <p>Thanks for all of the excellent answers! here's what I've resultingly come up with. No queries yet, just a simple dash of the read portion of CRUD, but the code will be the same, only with queries. A big thanks to @leepowers for introducing me to the wonderful parse_ini_file() function. </p> <pre><code>foreach(parse_ini_file("config/tones.txt", true) as $k =&gt; $v){ extract($v, EXTR_SKIP); echo "&lt;br&gt;"; echo $k . "&lt;br&gt;"; foreach($v as $sv =&gt; $ssv){ $lcase_sv = strtolower($sv); if (trim($lcase_sv) == 'amr_emails'){ echo "sv: amr_Emails:&lt;br&gt;"; echo "ssv:&lt;br&gt;"; $eA = explode(',', trim($ssv)); foreach($eA as $eK =&gt; $eV){ echo "email" . filter_var($eK + 1, FILTER_SANITIZE_NUMBER_INT) . ": " . $eV . "&lt;br&gt;"; } } elseif (trim($lcase_sv) == 'mp3_emails'){ echo "ssv:&lt;br&gt;"; $eA = explode(',', trim($ssv)); foreach($eA as $eK =&gt; $eV){ echo "email" . filter_var($eK + 1, FILTER_SANITIZE_NUMBER_INT) . ": " . $eV . "&lt;br&gt;"; } }else { echo "sv: " . $sv .", " . "s: " . $ssv . "&lt;br&gt;"; } } </code></pre> <p>} </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload