Null values in a database [SOLVED]

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
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Null values in a database [SOLVED]

Post by QbertsBrother »

so i have a table that has fields that are named the following.

option1, option2, option3 ..... option33, option34, option35

i am able to display the options just fine and i have the output like this

Code: Select all

 
".$result['option1'].", ".$result['option2'].", ".$result['option3'].", ".$result['option4'].", ".$result['option5']."
 
now that works great but the problem is that if the last 5 options are null or empty i get this at the end of my output

Code: Select all

 
,,,,,,,,,
 
what i am trying to do is create a loop that checks weather or not the field is null or empty and if it is empty dont display it and dont display the ,

i am having trouble as my loop is not working

Code: Select all

 
for ($options = 1; $options <= 35; $options++) {
if ($rowvehicles[o$avail_options] != ''){
$avail_options .= "".$result[option$avail_options].", ";
}
else {
$avail_options .= '';
}
}
 
any help would be great

thanks
Last edited by QbertsBrother on Mon Jun 30, 2008 2:34 pm, edited 1 time in total.
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: Null values in a database

Post by QbertsBrother »

i was able to figure it out

i changed the code to this

Code: Select all

 
for ($options = 1; $options <= 35; $options++) {
if ($rowvehicles['o'.$avail_options] != ''){
$avail_options .= "".$result['o'.ption$avail_options].", ";
}
else {
$avail_options .= '';
}
}
 
works

now i just have one extra , at the end of the option list
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Null values in a database

Post by califdon »

I'm glad you found a solution that satisfies your need. Now please do us a favor and return to your original posting and EDIT the Subject to include the word [SOLVED] so that others who are trying to help folks like you don't have to read through your entire posting before they discover that you no longer need their help. We will appreciate that.

An added comment: Your description of your table structure raises a serious flag to any experienced database designer. It just screams of unnormalized data. If it meets your needs, I won't belabor the point, but if this is something that is likely to be expanded, modified or duplicated, you would be well served to read something about the normalization of relational databases.
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: Null values in a database

Post by QbertsBrother »

califdon wrote: An added comment: Your description of your table structure raises a serious flag to any experienced database designer. It just screams of unnormalized data. If it meets your needs, I won't belabor the point, but if this is something that is likely to be expanded, modified or duplicated, you would be well served to read something about the normalization of relational databases.
thanks for your concern about my database structure. i would not create a table the way it is myself but i am actually getting the data from a 3rd party. that is the way they have set it up and i have no control of how the database is structured. i am loading the data into my database from a csv file that is uploaded to my server everyday. i have a job that takes the file and loads it into the table everyday.

i am not aware of a way to take the data from the csv file and change the way it is stored.

thanks
Post Reply