Jump to content

extract words in a file


homer.favenir

Recommended Posts

hi,

i have a difficult situation here (for me i think),

the situation is:

i will upload a txt file using fopen(),

i have to find the line that begins with 1 in that file

e.g. 1AP785 2HS 426.00

 

inline with that 1, i have to extract each word.

e.g. AP785 2HS 426.00 (excluding the 1)

and each word that i will extract i will place them in variables.

e.g. $word1=AP785, $word2=2HS, $word3 = 426.00

 

cause i will insert it on a database...

 

pls help me!

thanks

:tntworth:

Link to comment
Share on other sites

Fairly simple:

 

$lines = file('data.txt');

foreach($lines as $line)
{
    $line = substr($line, 1, strlen(trim($line)));

   list($word1, $word2, $word3) = explode(' ', $line);

   echo 'Word1: ' . $word1 . '<br />';
   echo 'Word2: ' . $word2 . '<br />';
   echo 'Word3: ' . $word3;
}

Link to comment
Share on other sites

hi,

btw thanks for the reply....

your script is good.its really great.

 

the process is...

it will find a line that begins with 1,

get that line,

extract the words in that line and save it in variable,

the break is 20, when it sees 20 it stops in extracting.

search again for 1, extract words, break.

search again for 1, extract words, break.

search again for 1, extract words, break.

e.g. file:

003452302/08YCaro/Marlette              GANNETT                      2

1AB100  RL      0.00                                                                                                                                                                                                                                                                                                                                        1 

20

3NPositive Alternatives

3A1596 E Caro Rd

3CCaro

3P672-4673

1AC200  RL      0.00                                                                                                                                                                                                                                                                                                                                        1 

20

 

pls let me know your thoughts.

very much appreciated..

thankss!

Link to comment
Share on other sites

1AB100  RL      0.00

 

i can now extract these words, but i have to delete first the white spaces.

how can i trim them or delete?

 

this is  my script.

$lines = file('welcome.txt');

 

foreach($lines as $line)

if ($line[0] == "1")

{

 

$line = substr($line, 1, strlen(rtrim($line)));

list($word1, $word2, $word3) = explode(' ', $line);

 

echo 'Word1: ' . $word1 . '<br />';

echo 'Word2: ' . $word2 . '<br />';

echo 'Word3: ' . $word3 . '<br />';

 

}

 

this code works but the white spaces really is a problem...

pls...

thanks

Link to comment
Share on other sites

Edit Ignore that didn't read your post properly.

 

I used regex instead of a simple explode:

$lines = file('welcome.txt');

foreach($lines as $line)
{
    if(preg_match("|^1([a-z0-9]+)\s+([a-z]+)\s+([0-9\.]+)|i", $line, $matches))
    {
    list(,$word1, $word2, $word3) = $matches;

    	echo '<p>Word1: ' . $word1 . '<br />';
    	echo 'Word2: ' . $word2 . '<br />';
    	echo 'Word3: ' . $word3 . '</p>';
    }
}

Link to comment
Share on other sites

nice!....

i have one question...

how do you explode two or more separator?

i can explode with 1 separator but how about many?

e.g.

$data = "EL/93.00,EL/93.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00";
{
list($udac[1], $udac[2], $udac[3], $udac[4], $udac[5], $udac[6], $udac[7], $udac[8], $udac[9] ) = explode("/", $data);
echo $udac[0] . '<br />';
echo $udac[1] . '<br />'; // foo
echo $udac[2] . '<br />';
echo $udac[3] . '<br />';
echo $udac[4] . '<br />';
echo $udac[5] . '<br />';
echo $udac[6] . '<br />';
echo $udac[7] . '<br />';
echo $udac[8] . '<br />';
echo $udac[9] . '<br />';
}

this works for only 1 separator.

i use syntax: explode(separator,string,limit)

 

please advice...

thanks

???

 

Link to comment
Share on other sites

Is each piece of data (EL/93.00) separated by commas? If thats the case you'll first have to use explode(',',$data) to get each piece of data on its own then use a foreach loop to loop through the bits of data. Finaly within the loop use explode("/", $data) to separate the initials (EL or RLM) and 0.00 on their own. Put it all together and you end up with this:

<?php

$datas = 'EL/93.00,EL/93.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00';

// sperarate each peice of data from comma delimited list
$bits = explode(',', $datas);

// loop through the bits of data
foreach($bits as $data)
{
    // separate the initials (EL or RLM) and 0.00 into $udac array
    $udac = explode('/', $data);

    echo '<p>' . $udac[0] . '<br />' . $udac[1] . '</p>';
}

?>

Link to comment
Share on other sites

i made it work with your script,

but i have one more last question....

how can i remove the word between 132.00 and TMR/1239.00?

how can i remove State Farm and 41?

"1IN510  TRL    132.00  State Farm                                                                                                                                                                                                                                                                                                                          41  TMR/1239.00"

because there some lines with these words in between.

and they really messed up my work.

 

pls advice...

thanks

???

Link to comment
Share on other sites

<?php
$handle = @fopen("welcome.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);

        $words = array();

        if (preg_match('/^1/', $buffer)) {
            //remove the first letter
            $buffer = substr($buffer , 1);
            
            //replace the space(s) or tab(s) into 1 space only
            $buffer = preg_replace('/(\ +|\t+)/', ' ', trim($buffer));
    
            //get the per word
            $array = explode(" ", $buffer);
            
            //store into array instead
            foreach ($array as $value) {
                $words[] = $value;

    }

            //insert here the $words[] to db to prevent memory exhausted
   
   {
    	echo '<b>Heading Code:</b>' . $words[0] . '<br />';
	echo '<b>Udac Code:</b>' . $words[1] . '<br />';
	echo '<b>Udac Rate:</b>' . $words[2] . '<br />';

   	list($udac[1], $udac[2], $udac[3], $udac[4], $udac[5], $udac[6], $udac[7], $udac[8], $udac[9], $udac[10],
	     $udac[11], $udac[12], $udac[13], $udac[14], $udac[15], $udac[16], $udac[17], $udac[18], $udac[19], $udac[20],
	     $udac[21], $udac[22], $udac[23], $udac[24], $udac[25], $udac[26], $udac[27], $udac[28], $udac[29], $udac[30],
	     $udac[31], $udac[32], $udac[33], $udac[34], $udac[35], $udac[36], $udac[37], $udac[38], $udac[39], $udac[40],
	     $udac[41], $udac[42], $udac[43], $udac[44], $udac[45], $udac[46], $udac[47], $udac[48], $udac[49], $udac[50])= split('[/,]', $words[4]);
	  
	     		     		     
   }
	echo '<b>extraudac1:</b> ' . $i . $udac[$i] . '<br />';
	echo '<b>extraudacrate1</b>: ' . $udac[2] . '<br />';
	echo '<b>extraudac2: </b>' . $udac[3] . '<br />';
	echo '<b>extraudacrate2:</b> ' . $udac[4] . '<br />';
	echo '<b>extraudac3:</b> ' . $udac[5] . '<br />';
	echo '<b>extraudacrate3:</b> ' . $udac[6] . '<br />';
	echo '<b>extraudac4:</b> ' . $udac[7] . '<br />';
	echo '<b>extraudacrate4:</b> ' . $udac[8] . '<br />';
	echo '<b>extraudac5:</b> ' . $udac[9] . '<br />';
	echo '<b>extraudacrate5:</b> ' . $udac[10] . '<br />';
	echo '<b>extraudac6:</b> ' . $udac[11] . '<br />';
	echo '<b>extraudacrate6</b>: ' . $udac[12] . '<br />';
	echo '<b>extraudac7: </b>' . $udac[13] . '<br />';
	echo '<b>extraudacrate7:</b> ' . $udac[14] . '<br />';
	echo '<b>extraudac8:</b> ' . $udac[15] . '<br />';
	echo '<b>extraudacrate8:</b> ' . $udac[16] . '<br />';
	echo '<b>extraudac9:</b> ' . $udac[17] . '<br />';
	echo '<b>extraudacrate9:</b> ' . $udac[18] . '<br />';
	echo '<b>extraudac10:</b> ' . $udac[19] . '<br />';
	echo '<b>extraudacrate10:</b> ' . $udac[20] . '<br />';
	echo '<b>extraudac11:</b> ' . $udac[21] . '<br />';
	echo '<b>extraudacrate11</b>: ' . $udac[22] . '<br />';
	echo '<b>extraudac12: </b>' . $udac[23] . '<br />';
	echo '<b>extraudacrate12:</b> ' . $udac[24] . '<br />';
	echo '<b>extraudac13:</b> ' . $udac[25] . '<br />';
	echo '<b>extraudacrate13:</b> ' . $udac[26] . '<br />';
	echo '<b>extraudac14:</b> ' . $udac[27] . '<br />';
	echo '<b>extraudacrate14:</b> ' . $udac[28] . '<br />';
	echo '<b>extraudac15:</b> ' . $udac[29] . '<br />';
	echo '<b>extraudacrate15:</b> ' . $udac[30] . '<br />';
	echo '<b>extraudac16:</b> ' . $udac[31] . '<br />';
	echo '<b>extraudacrate16</b>: ' . $udac[32] . '<br />';
	echo '<b>extraudac17: </b>' . $udac[33] . '<br />';
	echo '<b>extraudacrate17:</b> ' . $udac[34] . '<br />';
	echo '<b>extraudac18:</b> ' . $udac[35] . '<br />';
	echo '<b>extraudacrate18:</b> ' . $udac[36] . '<br />';
	echo '<b>extraudac19:</b> ' . $udac[37] . '<br />';
	echo '<b>extraudacrate19:</b> ' . $udac[38] . '<br />';
	echo '<b>extraudac20:</b> ' . $udac[39] . '<br />';
	echo '<b>extraudacrate20:</b> ' . $udac[40] . '<br />';
	echo '<b>extraudac21:</b> ' . $udac[41] . '<br />';
	echo '<b>extraudacrate21</b>: ' . $udac[42] . '<br />';
	echo '<b>extraudac22: </b>' . $udac[43] . '<br />';
	echo '<b>extraudacrate22:</b> ' . $udac[44] . '<br />';
	echo '<b>extraudac23:</b> ' . $udac[45] . '<br />';
	echo '<b>extraudacrate23:</b> ' . $udac[46] . '<br />';
	echo '<b>extraudac24:</b> ' . $udac[47] . '<br />';
	echo '<b>extraudacrate24:</b> ' . $udac[48] . '<br />';
	echo '<b>extraudac25:</b> ' . $udac[49] . '<br />';
	echo '<b>extraudacrate25:</b> ' . $udac[50] . '<br />';

        }
}
    fclose($handle);
    }?>

 

and the output is:

Heading Code:CL490

Udac Code:2HS

Udac Rate:426.00

extraudac1:

extraudacrate1: 93.00

extraudac2: EL

extraudacrate2: 93.00

extraudac3: EL

extraudacrate3: 93.00

extraudac4: RLM

extraudacrate4: 0.00

extraudac5: RLM

extraudacrate5: 0.00

extraudac6:

extraudacrate6:

extraudac7:

extraudacrate7:

extraudac8:

extraudacrate8:

 

but it can't see the other text "TMR/1239.00" here:

"1IN510  TRL    132.00  State Farm                                                                                                                                                                                                                                                                                                                          41  TMR/1239.00"

i dont need "State Farm and 41" or any words that is between them...

pls help...

thanks

 

???

Link to comment
Share on other sites

Huh! So where's my code? Now I'm confused! What on earth is the following supposed to do:

list($udac[1], $udac[2], $udac[3], $udac[4], $udac[5], $udac[6], $udac[7], $udac[8], $udac[9], $udac[10],
	     $udac[11], $udac[12], $udac[13], $udac[14], $udac[15], $udac[16], $udac[17], $udac[18], $udac[19], $udac[20],
	     $udac[21], $udac[22], $udac[23], $udac[24], $udac[25], $udac[26], $udac[27], $udac[28], $udac[29], $udac[30],
	     $udac[31], $udac[32], $udac[33], $udac[34], $udac[35], $udac[36], $udac[37], $udac[38], $udac[39], $udac[40],
	     $udac[41], $udac[42], $udac[43], $udac[44], $udac[45], $udac[46], $udac[47], $udac[48], $udac[49], $udac[50])= split('[/,]', $words[4]);

 

We're back to square one now.

 

Please post the exact data in welcome.txt  (only a few lines will suffice) and then tell me what output you want.

Link to comment
Share on other sites

it splits the words "RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00"

and put it on variables.

 

welcome.txt file looks like this:

 

3NBank One

3A3064 Main

3CMarlette

3P635-7455

1LO100  5HSA    918.00                                                                                                                                                                                                                                                                                                                                      45  RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00,RLM/0.00

20

3NChase

21

3TConsumer English

3P800 935-9935

21

3TConsumer Spanish

3P877 312-4273

21

3TTDD

3P800 242-7583

21

3TCorporate & Commercial

3P800 341-3530

21

3TSmall Business

3P800 242-7338

21

3TNew Accounts

3P800 242-7524

1LO100  1MS    354.00                                                                                                                                                                                                                                                                                                                                      45  RLM/0.00,RLM/0.00

20

 

thanks

 

 

Link to comment
Share on other sites

the line that i need in the text file is this:

"1AP355  RL      0.00        STATE FARM            5        EL/93.00,EL/93.00,EL/93.00

20"

so the trigger is the 1 in 1AP355, if it finds 1 in the first character it copies the line.

it remove the 1 and get the AP355(heading code), RL(udaccode), 0.00(udacrate), ignore "STATE FARM and 5", get EL(extraudac),93.00(extraudacrate) and so on...and when it sees 20 it stops in extracting.back to procedure again from the beginning.and it loops untill feof.

i hope i explained it well...im not very good in english...C",)

thanks a lot...

 

Link to comment
Share on other sites

Ooh! I see now. Try:

<?php

$lines = file('welcome.txt');

foreach($lines as $line)
{
    // check the line starts with a number 1
    if(substr($line, 0, 1) == '1')
    {
        $line = substr($line, 1, strlen($line));

        //replace the space(s) or tab(s) into 1 space only
        $line = preg_replace('/(\ +|\t+)/', ' ', trim($line));

        //get the per word
        $words = explode(' ', $line);

    echo '<p><b>Heading Code:</b>' . $words[0] . '<br />';
        echo '<b>Udac Code:</b>' . $words[1] . '<br />';
        echo '<b>Udac Rate:</b>' . $words[2] . '<br />';


        // intelligently display remaining datas. Not need to do:
        /*
            @list($udac[1], ...etc ..., $udac[50])= split('[/,]', $words[4]);

            echo '<p><b>extraudac1:</b> ' . @$i . @$udac[$i] . '<br />';
            ... etc ...
            echo '<b>extraudacrate25:</b> ' . $udac[50] . '</p>';
        */

        // just use a simple explode and a foreach statement
        $i = 1;
        // sperarate each peice of data from comma delimited list
        foreach(explode(',', end($words)) as $data_bit)
        {
            $bit = explode('/', $data_bit);

            echo '<b>extraudac'.$i.':</b> ' . $bit[0] . '<br />';
            echo '<b>extraudacrate'.$i.'</b>: ' . $bit[1] . '<br />';
            $i++;
        }

        echo '</p>';
    }
}

?>

Link to comment
Share on other sites

your very good...thanks!

but one more thing...how do you insert it to table of a database.

this is my script...pls check...cause when i run it, it returns error.

<?php
//connect to database
    $con = mysql_connect("localhost","root","");
    	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("udacupload", $con);

$lines = file('welcome.txt');

foreach($lines as $line)
{
    // check the line starts with a number 1
    if(substr($line, 0, 1) == '1')
    {
        $line = substr($line, 1, strlen($line));

        //replace the space(s) or tab(s) into 1 space only
        $line = preg_replace('/(\ +|\t+)/', ' ', trim($line));

        //get the per word
        $words = explode(' ', $line);

    echo '<p><b>Heading Code:</b>' . $words[0] . '<br />';
        echo '<b>Udac Code:</b>' . $words[1] . '<br />';
        echo '<b>Udac Rate:</b>' . $words[2] . '<br />';


        // intelligently display remaining datas. Not need to do:
        /*
            @list($udac[1], ...etc ..., $udac[50])= split('[/,]', $words[4]);

            echo '<p><b>extraudac1:</b> ' . @$i . @$udac[$i] . '<br />';
            ... etc ...
            echo '<b>extraudacrate25:</b> ' . $udac[50] . '</p>';
        */

        // just use a simple explode and a foreach statement
        $i = 1;

        // sperarate each peice of data from comma delimited list
        foreach(explode(',', end($words)) as $data_bit)
        {
            $bit = explode('/', $data_bit);

            echo '<b>extraudac'.$i.':</b> ' . $bit[0] . '<br />';
            echo '<b>extraudacrate'.$i.'</b>: ' . $bit[1] . '<br />';
           
    $i++;
    	
    	$j=1;
	while($j<=20)
	{
		$sql = "INSERT INTO udac (extraudac[$j], extraudacrate[$j])
		VALUES
		('$bit[0]','$bit[1]')";
		if (!mysql_query($sql,$con))
		{
			die('Error: ' . mysql_error());
		}   
	}
}

$sql = "INSERT INTO udac (headingcode, udaccode, udacrate)
		VALUES
		('$words[0]','$words[1]','$words[2]')";
		if (!mysql_query($sql,$con))
		{
			die('Error: ' . mysql_error());
		}   
        echo '</p>';
    }
    
    
}?>

Link to comment
Share on other sites

this is my database

-- MySQL Administrator dump 1.4
--
-- ------------------------------------------------------
-- Server version	5.0.51


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;


--
-- Create schema udacupload
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ udacupload;
USE udacupload;

--
-- Table structure for table `udacupload`.`udac`
--

DROP TABLE IF EXISTS `udac`;
CREATE TABLE `udac` (
  `idudac` int(10) unsigned NOT NULL auto_increment,
  `HeadingCode` varchar(45) NOT NULL default '',
  `UdacCode` varchar(45) NOT NULL default '',
  `UdacRate` int(10) unsigned NOT NULL default '0',
  `extraudac1` varchar(45) NOT NULL default '',
  `extraudacrate1` int(10) unsigned NOT NULL default '0',
  `extraudac2` varchar(45) NOT NULL default '',
  `extraudacrate2` int(10) unsigned NOT NULL default '0',
  `extraudac3` varchar(45) NOT NULL default '',
  `extraudacrate3` int(10) unsigned NOT NULL default '0',
  `extraudac4` varchar(45) NOT NULL default '',
  `extraudacrate4` int(10) unsigned NOT NULL default '0',
  `extraudac5` varchar(45) NOT NULL default '',
  `extraudacrate5` int(10) unsigned NOT NULL default '0',
  `extraudac6` varchar(45) NOT NULL default '',
  `extraudacrate6` int(10) unsigned NOT NULL default '0',
  `extraudac7` varchar(45) NOT NULL default '',
  `extraudacrate7` int(10) unsigned NOT NULL default '0',
  `extraudac8` varchar(45) NOT NULL default '',
  `extraudacrate8` int(10) unsigned NOT NULL default '0',
  `extraudac9` varchar(45) NOT NULL default '',
  `extraudacrate9` int(10) unsigned NOT NULL default '0',
  `extraudac10` varchar(45) NOT NULL default '',
  `extraudacrate10` int(10) unsigned NOT NULL default '0',
  `extraudac11` varchar(45) NOT NULL default '',
  `extraudacrate11` int(10) unsigned NOT NULL default '0',
  `extraudac12` varchar(45) NOT NULL default '',
  `extraudacrate12` int(10) unsigned NOT NULL default '0',
  `extraudac13` varchar(45) NOT NULL default '',
  `extraudacrate13` int(10) unsigned NOT NULL default '0',
  `extraudac14` varchar(45) NOT NULL default '',
  `extraudacrate14` int(10) unsigned NOT NULL default '0',
  `extraudac15` varchar(45) NOT NULL default '',
  `extraudacrate15` int(10) unsigned NOT NULL default '0',
  `extraudac16` varchar(45) NOT NULL default '',
  `extraudacrate16` int(10) unsigned NOT NULL default '0',
  `extraudac17` varchar(45) NOT NULL default '',
  `extraudacrate17` int(10) unsigned NOT NULL default '0',
  `extraudac18` varchar(45) NOT NULL default '',
  `extraudacrate18` int(10) unsigned NOT NULL default '0',
  `extraudac19` varchar(45) NOT NULL default '',
  `extraudacrate19` int(10) unsigned NOT NULL default '0',
  `extraudac20` varchar(45) NOT NULL default '',
  `extraudacrate20` int(10) unsigned NOT NULL default '0',
  `extraudac21` varchar(45) NOT NULL default '',
  `extraudacrate21` int(10) unsigned NOT NULL default '0',
  `extraudac22` varchar(45) NOT NULL default '',
  `extraudacrate22` int(10) unsigned NOT NULL default '0',
  `extraudac23` varchar(45) NOT NULL default '',
  `extraudacrate23` int(10) unsigned NOT NULL default '0',
  `extraudac24` varchar(45) NOT NULL default '',
  `extraudacrate24` int(10) unsigned NOT NULL default '0',
  `extraudac25` varchar(45) NOT NULL default '',
  `extraudacrate25` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`idudac`),
  KEY `Index_2` (`HeadingCode`,`UdacCode`,`UdacRate`)
) ENGINE=InnoDB AUTO_INCREMENT=27084 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `udacupload`.`udac`
--

/*!40000 ALTER TABLE `udac` DISABLE KEYS */;
/*!40000 ALTER TABLE `udac` ENABLE KEYS */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

 

Link to comment
Share on other sites

Can you post your database schema for the udac table

 

Okay added in the neccessary code to insert data into a database:

<?php
//connect to database                           display error if connection fails
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("udacupload", $con);

$lines = file('welcome.txt');

foreach($lines as $line)
{
    // check the line starts with a number 1
    if(substr($line, 0, 1) == '1')
    {
        $line = substr($line, 1, strlen($line));

        //replace the space(s) or tab(s) into 1 space only
        $line = preg_replace('/(\ +|\t+)/', ' ', trim($line));

        //get the per word
        $words = explode(' ', $line);

    echo '<p><b>Heading Code:</b>' . $words[0] . '<br />';
        echo '<b>Udac Code:</b>' . $words[1] . '<br />';
        echo '<b>Udac Rate:</b>' . $words[2] . '<br />';

        // start sql query
        $sql = "INSERT INTO udac SET headingcode='$words[0]', udaccode='$words[1]', udacrate='$words[2]'";

        $i = 1;
        // sperarate each peice of data from comma delimited list
        foreach(explode(',', end($words)) as $data_bit)
        {
            $bit = explode('/', $data_bit);

            echo '<b>extraudac'.$i.':</b> ' . $bit[0] . '<br />';
            echo '<b>extraudacrate'.$i.'</b>: ' . $bit[1] . '<br />';

            // dynamically generate the query string
            $sql .= ", extraudac{$i}='$bit[0]', extraudacrate$i='$bit[1]'";

            $i++; // increment counter
	}

        // display SQL query to be ran
        echo '<p>SQL Query: <pre>'.$sql.'</pre>';
        // run query                display error if query fails
        mysql_query($sql) or die('Error: ' . mysql_error());
        // display success when query runs ok.
        echo 'SUCCESS!</p>';

        echo '</p>';
    }
}

?>

Link to comment
Share on other sites

how do you manage to be good in php?

if you dont mind, can you please give me tips to be so good like you in php? ;D

 

thanks ... ;D

Glad I helped.

 

The best way to be a good PHP programmer is to have a thorough understanding of the basics, such as variables, control structures, loops, operators etc. All this can be learnt by reading the easy to follow manual. Without this you'll find it hard to understand the flow/logic of the script.

 

Everytime you come up to a part in the code that you don't understand, especially if its a functions. Just open your browser and go to http://php.net/function_name eg http://php.net/substr will take you to the page that tells you how substr() works and provides simple to follow code examples. I find myself doing this most of the time as I usually forget the parameter order for a specific function.

 

Another thing to do is to get a script, and literally break it and then see if you can fix it on your own, or look at other (basic) scripts and seeing how they work, then go about recoding it in a different way or try to add extra features to it. When I started PHP I remember reading a tutorial at sitepoint.com which taught me how to make a simple Jokes Database, it wasn't advanced or too hard and the tutorial was explained very well throughout. After reading the tutorial I sort of understood what was happening as I had a good understanding of the basics of PHP, this then allowed me to convery it from a jokes database into a very simple forum nothing special but it was an achievement.

 

You cant become a PHP expert in a day/week/month. PHP has a very long learning curve.

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.