PHP GD MySQL Problem

Discussion in 'PHP' started by nathan100, Aug 10, 2007.

  1. #1
    Hi,
    Trying to add GD Line chart to this that draws a chart depending on the result of the query.... Thechart needs to update each time the user selects new data from the combo box to populate the table....

    Iv had a go - but it jsut wont work! it keeps asking me to open/save/cancel , so i took it out!!


    <html>
    <head>
    <title>World Records</title>
    </head>
    <body>
    <?php
    	$con = mysql_connect("host","user","pass");
    	if (!$con) {
    	 	die('Could not connect: ' . mysql_error());
    	}
    	
    	mysql_select_db("database", $con);
    ?>
    
    <form action="worldrecords.php?submit=yes" method="post">
    <?php 
    	echo "<select name='event'>";
    
    	$res=mysql_query("select DISTINCT event from worldrecords");
    
    	if(mysql_num_rows($res)==0) {
    		echo "there is no data in table..";
    	} else {
    		while($row=mysql_fetch_assoc($res)) {
    			
    			echo "<option value='".$row['event']."'>".$row['event']."</option>";
    		}
    		echo '</select>';
    	
    		echo "<select name='gender'>";
    		$res2=mysql_query("select DISTINCT gender from worldrecords");
    	
    		if(mysql_num_rows($res2)==0) {
    			echo "there is no data in table..";
    		} else {
    			while($row2=mysql_fetch_assoc($res2)) {
    				echo "<option value='".$row2['gender']."'>".$row2['gender']."</option>";
    			}
    		}
    	}
    	echo '</select>';
    ?>
    		<input type="submit" name="submit" value="submit">
    	</form>
    <?php
    	if($_GET['submit']) {	
    		$event=$_REQUEST['event'];
    		$gender=$_REQUEST['gender'];
    		
    		$mySql="SELECT * FROM worldrecords WHERE (event='".$event."') AND (gender='".$gender."') ORDER BY year, athlete";
    		$result=mysql_query($mySql);
    		
    		echo "<h3>".$event."&nbsp;-&nbsp;".$gender."</h3>";
    		
    		if(mysql_num_rows($result)==0) {
    			echo "no records found..";
    		} else {
    ?>
    			<table border="1">
    				<tr>
    					<th>Year</th>
    					<th>Time</th>
    					<th>Venue</th>
    					<th>Athlete</th>
    					<th>Event</th>
    					<th>Pool</th>
    					<th>Gender</th>
    				</tr>
    <?php
    			while($row2=mysql_fetch_assoc($result)) {
    				echo "<tr>";
    				echo "<td>" . $row2['year'] . "</td>";
    				echo "<td>" . $row2['time'] . "</td>";
    				echo "<td>" . $row2['venue'] . "</td>";
    				echo "<td>" . $row2['athlete'] . "</td>";
    				echo "<td>" . $row2['event'] . "</td>";
    				echo "<td>" . $row2['pool'] . "</td>";
    				echo "<td>" . $row2['gender'] . "</td>";
    				echo "</tr>";
    			}
    ?>
    
    
    
    
    
    			</table>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    <?php
    		}
    	}
    	mysql_close($con);
    ?>
    
    
    
    </body>
    </html>
    PHP:
     
    nathan100, Aug 10, 2007 IP
  2. nathan100

    nathan100 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is the code i have tried to use to create the line chart, but it is not working... any ideas?

    
    <?php
    
    header ("Content-type: image/jpg");
    
    
    $x_gap=40; // The gap between each point in y axis 
    
    $x_max=$x_gap*13; // Maximum width of the graph or horizontal axis
    $y_max=250; // Maximum hight of the graph or vertical axis
    // Above two variables will be used to create a canvas of the image//
    
    
    $im = @ImageCreate ($x_max, $y_max)
    or die ("Cannot Initialize new GD image stream");
    $background_color = ImageColorAllocate ($im, 234, 234, 234);
    $text_color = ImageColorAllocate ($im, 233, 14, 91);
    $graph_color = ImageColorAllocate ($im,25,25,25);
    
    
    $x1=0;
    $y1=0;
    $first_one="yes";
    while($nt=mysql_fetch_array($result)){
    $x2=$x1+$x_gap; // Shifting in X axis
    $y2=$y_max-$nt[time]; // Coordinate of Y axis
    ImageString($im,2,$x2,$y2,$nt[athlete],$graph_color); 
    if($first_one=="no"){ // this is to prevent from starting $x1= and $y1=0
    imageline ($im,$x1, $y1,$x2,$y2,$text_color); // Drawing the line between two points
    }
    $x1=$x2; // Storing the value for next draw
    $y1=$y2;
    $first_one="no"; // Now flag is set to allow the drawing
    }
    
    ImageJPEG ($im);
    
    ?>
    
    
    PHP:
     
    nathan100, Aug 11, 2007 IP
  3. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What is in the file that you are asked to Save? And also make sure there are no extra spaces or newlines at the end of or the beginning of your PHP files. That can cause Image generation to break.

    ~
    Thomas
     
    coderlinks, Aug 12, 2007 IP