Jump to content

Inserting code if field is not in database?


Travis959

Recommended Posts

This is kind of hard to explain, but I will try my best.

 

I have a script that is attempting to display information in a MySQL database by month fields that are chosen by the user. For instance, they can choose for the start date to start in January, and pick the end date which in this example will be April.

 

The script then puts the required field that match the specific dates into a column for each month. For the most part it outputs fine as: Jan: 15, Feb: 14, Mar: 9, Apr: 2.

 

However, sometimes there is nothing in the database for certain months. This causes the script to display wrong so instead I get: Jan 3, Feb: , Mar: , Apr: .

 

In the above example, the Jan: 3 might be wrong, because the field might have been for Mar instead. I need it to display like this when there is no fields in the database: Jan: 0, Feb: 0, Mar: 3, Apr: 0.

 

How would I go about doing this?

This is the code I'm using. I'm not sure how the above code is supposed to figure out where there are no rows at.  Anyways, it's still not working.

 

Also, my $i7 variable is not getting incremented with "$i7++", it always returns 1 regardless, as I was going to compare the $i7 to the month field in the database.

 

             

$startmonth2 = ltrim($startmonth, '0');	
                        $i7 = $startmonth2;
                        $searchdatabase8 = "SELECT * FROM slssum2 WHERE company = '$companyname' AND CONCAT(year,month) BETWEEN '$startyear$startmonth' AND '$endyear$endmonth' AND field3 = '$field6_3' AND field4 = '$field6_4' AND field5 = '$field6_5' AND month = '$field7_month' AND year = '$field7_year' ORDER BY field3,field4,year,month ASC";
		$run8 = mysql_query($searchdatabase8) or die (mysql_error());			
		while($row8 = mysql_fetch_assoc($run8))
		{ 	

			$field8_10 = $row8['field10'];
			$field8_month = ltrim($row8['month'], '0');
			if(mysql_num_rows($row8) > 0){
				echo '<td bgcolor="#ccffcc" width="25px"><strong>'; echo $field8_10; echo '</strong></td>';		
			} else {					
				echo '<td bgcolor="#ccffcc" width="25px">0</td>';			
			}
			$i7++;				
		}
	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.