Page 1 of 1

Null values in a database [SOLVED]

Posted: Mon Jun 30, 2008 1:25 pm
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

Re: Null values in a database

Posted: Mon Jun 30, 2008 1:59 pm
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

Re: Null values in a database

Posted: Mon Jun 30, 2008 2:26 pm
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.

Re: Null values in a database

Posted: Mon Jun 30, 2008 2:37 pm
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