Forum Moderators: open
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
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]
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.
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.
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
}