Page 1 of 1

[Solved] Explode problems...

Posted: Thu Jul 12, 2007 1:16 pm
by Dale
Ok, so I'm using the explode() tag to make an array each side of the | character.

eg;

Code: Select all

First Day at School|http://www.foo.bar/image1.html
Sports Day at School|http://www.foo.bar/image2.html
Dale in Newspaper|http://www.foo.bar/image3.html
However when I launch the little script to make it easier for me to add it into a database it just messes up, shows the first line and a tiny bit of the next title, only showing this:

Code: Select all

INSERT INTO `pics` VALUES('','First Day at School','http://www.foo.bar/image1.html
Sports Day at School');
Here is the code I'm using:

Code: Select all

<?php
if($_POST[tgpst]) {
$rep = explode("|", $_POST[tgpst]);
$re = "INSERT INTO `pids` VALUES('','$rep[0]','$rep[1]');";
?><textarea cols="100" rows="15" name="tgpst" style="border:5px solid #990000;padding:5px;"><?php print $re; ?></textarea><hr /><?php
}
?>
<form action="<? echo $_SERVER['PHP_SELF'] ?>" method="POST"><textarea cols="100" rows="15" name="tgpst"></textarea><br /><input type="submit" name="search" value="Generate..." /></form>
Any ideas?

(Oh and it basically prints the INSERT information on the page so I can just copy and paste it into phpMyAdmin's SQL box.

Posted: Thu Jul 12, 2007 1:27 pm
by icesolid
Are you trying to add more than one record to the database at a time?

If so you need to use a loop such as while(); to insert each individual record in the database.

One more thing, why don't you let PHP insert the records into the database?

Posted: Thu Jul 12, 2007 1:39 pm
by Dale
icesolid wrote:Are you trying to add more than one record to the database at a time?

If so you need to use a loop such as while(); to insert each individual record in the database.

One more thing, why don't you let PHP insert the records into the database?
Yeah, I'm trying to make it turn out like this on the page:

Code: Select all

INSERT INTO `pics` VALUES('','First Day at School','http://www.foo.bar/image1.html');
INSERT INTO `pics` VALUES('','Sports Day at School','http://www.foo.bar/image2.html');
INSERT INTO `pics` VALUES('','Dale in the Newspaper','http://www.foo.bar/image3.html');
Also, I prefer to copy and paste the entries in manually with the above code, not sure why.. it's just a natural habit. :p

Posted: Thu Jul 12, 2007 1:45 pm
by mentor
First you have to explode separate the values on newline and then use again explode on that array values one by one, e.g.

explode

Code: Select all

$lines = explode("\n", $_POST[tgpst]); 
foreach($lines as $line)
{
  $rep = explode("|", $line); 
  $re = "INSERT INTO `pids` VALUES('','$rep[0]','$rep[1]');"; 
}

Posted: Thu Jul 12, 2007 1:59 pm
by Dale
mentor wrote:First you have to explode separate the values on newline and then use again explode on that array values one by one, e.g.

explode

Code: Select all

$lines = explode("\n", $_POST[tgpst]); 
foreach($lines as $line)
{
  $rep = explode("|", $line); 
  $re = "INSERT INTO `pids` VALUES('','$rep[0]','$rep[1]');"; 
}
Cheers dude. Got it working now. ;)