PDA

View Full Version : how to use implode and explode?...


ace04
02-08-2006, 04:26 AM
can anybody help me about implode and explode..
i have inserted my data using implode and i have made use of
"-" as a sign... how can i view this???....

MRMAN
02-08-2006, 10:24 AM
Well i think implode and explode are part of php and not mysql but ill answer as best i can.

this is what i would do to view the date with a date/time.


list($date, $time) = explode(" ",$datetime);
list($year, $month, $day) = explode("-",$date);
list($hour,$min,$sec) = explode(":",$time);

print "Date: " . $day . "/". $month . "/" . $year . "<BR>Time: " . $hour . ":" . $min . ":" . $sec;

that will print the date in the correct format and the time after that.

Hope this is what you are looking for

MRMAN

ace04
02-09-2006, 09:22 AM
yap!!! thank you very much..
godblesss...
:thumbsup:

ace04
02-22-2006, 06:01 AM
i'm having prblm executing this one:

for ($i="1"; $i<$dependent; $i++)
{
if(!checkifchar($dname[$i]))
{
$dname[$i]="";
}
unset($nb_dname);
$dname = implode("|", $dname);
$dage = implode("|", $dage);
$dschool = implode("|", $dschool);
$dyrlevel = implode("|", $dyrlevel);

$qdependent = "INSERT INTO dependent ('dependentID','dname','dage','dschool','dyrlevel' )
VALUES ('$i','$dname','$dage','$dschool','$dyrlevel')";
dbQuery($qdependent);

when i first use this.. it actually saves into mysql.. but now when im going to execute it again.. it can't insert into mysql anymore.. can you please give me an explanations or on how to solve this problem?.. thanx and godbless...
:confused:

StupidRalph
02-22-2006, 07:18 AM
You should ask a moderator to move this post over to the PHP forum as that is where your problem lies..NOT MySQL. Also, post your code wrapped in the PHP tags so it shows up formatted so its easier to read like this.

for ($i="1"; $i<$dependent; $i++)
{
if(!checkifchar($dname[$i]))
{
$dname[$i]="";
}
unset($nb_dname);
$dname = implode("|", $dname);
$dage = implode("|", $dage);
$dschool = implode("|", $dschool);
$dyrlevel = implode("|", $dyrlevel);

$qdependent = "INSERT INTO dependent ('dependentID','dname','dage','dschool','dyrlevel' )
VALUES ('$i','$dname','$dage','$dschool','$dyrlevel')";
dbQuery($qdependent);

Can you post the complete code so I can see it?

mindlessLemming
02-22-2006, 08:26 AM
Sounds like you're trying to use the same ID twice.
Making more assumptions, your dependants table would most likely be using the 'dependentID' field as a primary key. As such, each ID must be unique. If you look at your table through a MySQL GUI such as phpMyAdmin, you should be able to set the dependentID field as 'AUTO_INCREMENT'. After doing that, you will no longer need to pass 'dependentID' in the SQL -- the next available ID will automatically be assigned to your new row.

If you know your dependentID already exists and you want to change the dependant's data, you will need to use UPDATE instead of INSERT :)

Also, unless you're being *cough*highly creative*cough* with your ID field, it is most probably a number/integer, not a string. By enclosing the value in quotes within the SQL statement, you are indicating to MySQL that it is a string, not a number.

Fortunately/Unfortunately (depending on your view), the developers of MySQL seemingly wanted a low bar of entry for their DB so they included the ability to pass an integer to an integer field as a string and still have it store properly. Relying on this is very bad programming practice and worth avoiding early on.


Here's your code with all numbers being treated as numbers instead of strings.

// is $dependant a number?? it would want to be...
for ($i=1; $i<$dependent; $i++)
{
if(!checkifchar($dname[$i])) {
$dname[$i]="";
}
unset($nb_dname);
$dname = implode("|", $dname);
$dage = implode("|", $dage);
$dschool = implode("|", $dschool);
$dyrlevel = implode("|", $dyrlevel);

$qdependent = "INSERT INTO dependent ('dependentID','dname','dage','dschool','dyrlevel' )
VALUES ($i,'$dname','$dage','$dschool','$dyrlevel')";
dbQuery($qdependent);
}


Getting off track a little, your DB design is a little poor and lacks granularity.
Storing imploded arrays of data in single fields rather than designing your database tables to properly house and index ALL the application's data will bring you much grief. It's only a matter of time... Unless you love hammering your MySQL server with 'LIKE' queries...

Instead of storing all dependants in a single table row, you could make a table such as:


CREATE TABLE dependants (
parent_id INT(7) # IMPORTANT! parent_id is the ID from your main user's table (or whatever the parent of dependants is)
first_name VARCHAR(256), # allows 256 character length for safety sake...
last_name VARCHAR(256),
age INT(3), # I doubt you'll have anyone older than 999 ;)
school_id INT(7), # Don't store english words when you can store IDs that reference more complete data within another table. eg school name, address, phone number...
year_level(7)
);


Then you use the ID of the parent entry (what's above a dependant? a Person?) to associate all dependants with their parent.

Doing that allows you to do much neater stuff with your data.

Want the first name of the oldest child of the Person with ID 203? too easy:

SELECT first_name FROM dependants WHERE parent_id = 203 ORDER BY age DESC LIMIT 1


Done.

Try doing that sort of thing with your current database design... you can, but it's painful and slow. :)

reads original question: "can anybody help me about implode and explode.. "
Oh boy, my rant was way off! ;)