Welcome to WebmasterWorld Guest from 35.175.191.168

Forum Moderators: open

Message Too Old, No Replies

MySQL End Tag?

     
2:05 pm on Jun 26, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:May 7, 2008
posts:45
votes: 0


So I have a php script that tells MySQL to post 30 lines but sometimes I don't want it to post all 30 lines and to stop at say the 15th line depending on something.

Here is what it looks like (Yes it's messy but it's what I need):
{
$query = "INSERT INTO albuminfo(track,
details,
dl,
user,
trackn,
genre,
theyear,
art,
artist,
albumname)
VALUES ('$track_1', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_2', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_3', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_4', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_5', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_6', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_7', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_8', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_9', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_10', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_11', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_12', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_13', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_14', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_15', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_16', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_17', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_18', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_19', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_20', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_21', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_22', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_23', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_24', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_25', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_26', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_27', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_28', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_29', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname'),
('$track_30', '$details', '$dl', '$user', '$trackn', '$genre', '$theyear', '$art', '$artist', '$albumname')";
}
{
mysql_query($query) or die('YOUR FIELD WAS TO LONG!');
exit;
} }

So what I want to happen is for if track_24 is empty then to stop there and not post anymore lines. Seems simple enough but I just can't figure it out! Please help!

Thanks,
Spiceydog

2:29 pm on June 26, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2643
votes: 6


Put it in a loop man...

Make an array called 'data' that has all the tracks in it.. Then loop through data and test the value of track... if track has a value then perform insert if not.. break out of the loop.

Looks kinda like this....

for record in data {
if record['track'].has_value() {
$query = "INSERT INTO albuminfo(track,
details,
dl,
user,
trackn,
genre,
theyear,
art,
artist,
albumname)
VALUES ('$record['track']', '$record['details']', '$record['dl']', '$record['user']', '$record['trackn']', '$record['genre']', '$record['theyear']', '$record['art']', '$record['artist']', '$record['albumname']')

mysql_query($query) or die('YOUR FIELD WAS TO LONG!');
}else {break loop;}
}

Sorry my PHP sucks hopefully you get the idea.

[edited by: Demaestro at 2:30 pm (utc) on June 26, 2008]

2:57 pm on June 26, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:May 7, 2008
posts:45
votes: 0


that sounds like a great idea... however I don't think there is a "has_value" command for php....
3:03 pm on June 26, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2643
votes: 6


There should be a few ways to test it.

if you can test the length of it and it is an empty string something like this

if $record['track'].length() != 0

or

if it is a string you can check it it is empty

if $record['track'] != ""

there should be a few ways of checking.. it all depends on the data type.

4:26 pm on June 26, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:May 7, 2008
posts:45
votes: 0


what is all of this stuff about $record['track'] you keep talking about? What is so special about $record?
2:34 am on June 27, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2643
votes: 6


$record.... That was the name of a variable that I was assigning when looping through the an array named data in my example... Again I am not a PHP guy but I can tell you that the issue isn't an SQL one it is a PHP one.

What you need help doing is looping through all your tracks and testing for if a value exists... if yes inserting them one at a time until there is no value.

4:20 pm on June 27, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Demaestro was providing logic, not cut and paste code.

Just read in what's in the form. Only insert if data is present. Let's say you have 20 form fields for each track. The logic would be something like this:

Form objects:
Track #: <input type="text" name="track1">
Details: <input type="text" name="details1">
Album: <input type="text" name="albumname1">
... and so on, for all input fields

Track #: <input type="text" name="track2">
Details: <input type="text" name="details2">
Album: <input type="text" name="albumname2">
... and so on, for all input fields

Track #: <input type="text" name="track3">
Details: <input type="text" name="details3">
Album: <input type="text" name="albumname3">
... and so on, for all input fields

.... on up to 20

When it submits, loop through 20 times and look for instances of track[number] that are not submitted blank. Store them in an array, call it record:


for $i (1..20) {
# create the form name so we can look for it
# (track1, track2, etc., up to 20)
$trackfield = 'track'.$i;
# example, if form "track1" is not blank, save it for insert later
if ($_POST($trackfield} != '') {
$record[$i] = $_POST($trackfield);
}
}

So the length or the array $record is only going to be as long as you need it. In reality, you might want your "if" to check all fields for each track, not just track number.

Now you do the loop and insert as instructed above, but construct each of the form field names on the fly. Store these in variables so you have a unique set for each row:


for each $i ($record) {
# create the variables for form objects
# for each row
$trackfield = 'track'.$i; # "track1,track2 . . . "
$details = 'details'.$i; # "details1,details2 . . . "
$albumname = 'albumname'.$i; # "albumname1, albumname2 . . .
# and so on, for all fields in each row
$query = "INSERT INTO albuminfo (track,details,albumname....) VALUES
('$_POST[$trackfield]', '$_POST[$details]', '$_POST[$albumname]', ....)
# The execute must be located in this loop
}