[Solved] Explode problems...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

[Solved] Explode problems...

Post 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.
Last edited by Dale on Thu Jul 12, 2007 1:59 pm, edited 1 time in total.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post 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?
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post 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]');"; 
}
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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. ;)
Post Reply