Jump to content

Gazz1982

Members
  • Posts

    83
  • Joined

  • Last visited

Everything posted by Gazz1982

  1. Simply solved, all I had to do was change the name of the function, solved!
  2. Hi, I'm using the below code for a list, I want to do this again for a second query (drop down), on the same page eg. using: $sql2="SELECT statement2" $result2=mysql_query($sql2); I have tried renaming the variables but it just leads to a blank screen, where have I gone wrong? Thanks. ... $sql1="SELECT Questions.Question_ID, Questions.Question from Questions ORDER BY `Questions`.`Question_ID` ASC"; $result1=mysql_query($sql1); $site_array = array(); while (list($id, $name) = mysql_fetch_row($result1)) {$site_array[$id] = $name;} function get_options($arr, $current=null) { $opts = ''; foreach ($arr as $k => $v) { $sel = $k==$current ? 'selected="selected"' : ''; $opts .= "<option value='$k' $sel>$k $v</option>\n"; } return $opts; } ... ... <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <select name="site" onchange="this.form.submit();"> <?php echo"<option>Select Question</option>";?> <?php echo get_options($site_array);?> </select> </form> ... The above code does exactly what I want it to do, I duplicated the code as follows: $aspect_array = array(); while (list($id2, $name2) = mysql_fetch_row($result2)) {$aspect_array[$id2] = $name2;} function get_options($arr2, $current2=null) { $opts2 = ''; foreach ($arr2 as $k2 => $v2) { $sel2 = $k2==$current2 ? 'selected="selected"' : ''; $opts2 .= "<option value='$k2' $sel2>$k2 $v2</option>\n"; } return $opts2; } ... ... <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <select name="aspect" onchange="this.form.submit();"> <?php echo"<option>Select Aspect</option>";?> <?php echo get_options($aspect_array);?> </select> </form> ...
  3. ok sometimes I'm as thick as a post, just needed to put the $k after the $sel> $opts .= "<option value='$k' $sel>$k $v</option>\n";
  4. Thanks, now it works better, although I'm still not getting the Site_ID from the while (list($id, $name) = mysql_fetch_row($result1)) {$site_array[$id] = $name;} I would like this: 1 sitename 2 sitename 3 sitename 4 sitename where the number is the site_ID, so two columns in the option dropdown
  5. And solved, remove the value='k' and it works Although still it only gives the Sites.site_name_1 and not the Sites.site_id
  6. ok the problem lies in this line, it is returning the k as set in this value: $opts .= "<option value='k' $sel>$v</option>\n";
  7. Ok, so I had an error in my onchange statement, now it kind of works but the returned value is 'k' and not the values which should be returned, heres the updated code: <?php $sql1="SELECT Site_ID, Site_name_1 FROM `Sites` ORDER BY Sites.Site_name_1 ASC"; $result1=mysql_query($sql1); $site_array = array(); while (list($id, $name) = mysql_fetch_row($result1)) {$site_array[$id] = $name;} function get_options($arr, $current=null) { $opts = ''; foreach ($arr as $k => $v) { $sel = $k==$current ? 'selected="selected"' : ''; $opts .= "<option value='k' $sel>$v</option>\n"; } return $opts; } if(isset($_POST['site'])) { echo $_POST['site']; } ?> <html> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <select name="site" onchange="this.form.submit();"> <?php echo get_options($site_array);?> </select> </form> </body> </html>
  8. Thanks, I spotted that error ($_POST['site'] after posting the reply. However it does not require a submit button, the onchange function is doing the submit once an option is selected from the list: onchange='this.form.submit' This was working before I altered the code, now I'm scratching my head...
  9. Thank you, that gets me half way there. Now I have added in the if(isset($_POST['site_array'])) to post the result onchange but it is not echoing the site_array. I only want the selected value from the array so I can use it in another sql query (see question part 2) I would also like it to display the Site_ID as well as the Site_name_1 in the drop down (this is less important) The changed code: <?php $sql1="SELECT Site_ID, Site_name_1 FROM `Sites`"; $result1=mysql_query($sql1); $site_array = array(); while (list($id, $name) = mysql_fetch_row($result1)) {$site_array[$id] = $name;} function get_options($arr, $current=null) { $opts = ''; foreach ($arr as $k => $v) { $sel = $k==$current ? 'selected="selected"' : ''; $opts .= "<option value='k' $sel>$v</option>\n"; } return $opts; } if(isset($_POST['site_array'])) { echo $_POST['site_array']; } ?> <html> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <select name="site" onchange="this.form.submit"> <?php echo get_options($site_array);?> </select> </form> </body> </html>
  10. Hi all, I was hoping you could point me in the right direction, I have 2 questions. 1. I want to have an option drop down which uses the array of a returned sql query. I have the following code which uses onchange to submit the selection, how do I use the mysql_fetch_array($result1) to give a list of the two selected columns in the $Site=array() bit. 2. The selected result will be used in more SQL select statements, so once a site from the list is selected the relevant data from other tables will be displayed using e.g. select person from people [some inner join statement] where site="site 1"; Therefore the $_POST value must be available to be passed to other queries once selected. Thanks for any help, I still feel like a noob, but I'm getting there. Gary <?php include 'header.php'; ?> <div class='container'> <?php include 'menu.php'; ?> <?php include 'connect.php'; ?> <?php $sql1="SELECT Sites.Site_ID, Sites.Site_name_1 FROM `Sites`"; $result1=mysql_query($sql1); ?> <?php function get_options() { $site=array('Site 1'=>'Site 1', 'Site 2'=>'Site 2', 'Site 3'=>'Site 3'); $options=''; while(list($k,$v)=each($site)) { $options.='<option value="'.$v.'">'.$k.'</option>'; } return $options; } if(isset($_POST['site'])) { echo $_POST['site']; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <select name="site" onchange="this.form.submit();"> <?php echo get_options(); ?> </select>
  11. Problem solved, I decided to only allow the editing of single records, the coding from the example seemed wrong so here is what I did: It still needs alteration and tidying. list_records.php $username="xxxx"; // Mysql username $password="xxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="test_mysql"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); ?> <table width="400" border="1" cellspacing="0" cellpadding="3"> <tr> <td >Name</td> <td >Lastname</td> <td >Email</td> <td >Update</td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><?php echo $rows['name']; ?></td> <td><?php echo $rows['lastname']; ?></td> <td><?php echo $rows['email']; ?></td> // link to update.php and send value of id <?php echo "<td><a href='update.php?id=". $rows["id"] ."'>update</a></td>"; ?> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); update.php <?php $host="xxxx"; // Host name $username="xxxx"; // Mysql username $password="xxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="test_mysql"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['id']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result);?> <?php echo " <form name='form1' method='post' action='update_ac.php'> <table width='400' border='1' cellspacing='1' cellpadding='0'> <tr> <td>Name</td> <td>Lastname</td> <td>Email</td> </tr> "; ?> <?php echo "<tr>"; echo "<td ><input name='name' type='text' id='name' value='". $rows['name'] ."'></td>"; echo "<td ><input name='lastname' type='text' id='lastname' value='". $rows['lastname'] ."'></td>"; echo "<td ><input name='email' type='text' id='email' value='". $rows['email'] ."'></td>"; ?> </tr> <tr> <?php echo "<td><input name='id' type='hidden' id='id' value='". $rows['id'] ."'></td>"; echo "<td ><input type='submit' name='Submit' value='Submit'></td>"; ?> <?php echo " </tr> </table> </td> </form> "; ?> <?php // close connection mysql_close(); ?> update_ac.php <?php //include '../connect.php'; $host="xxxx"; // Host name $username="xxxx"; // Mysql username $password="xxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="test_mysql"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $id=$_POST["id"]; $name=$_POST["name"]; $lastname=$_POST["lastname"]; $email=$_POST["email"]; // update data in mysql database //$sql="UPDATE $tbl_name SET name='".$name."', lastname='".$lastname."', email='".$email."' WHERE id='$id'"; $sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'"; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_records.php'>View result</a>"; print "$name"; } else { echo "ERROR"; } ?> <?php mysql_close(); ?>
  12. I have been trying to use some code to update a simple table on multiple columns. For some reason the code below will present the data but if I alter the details the data is not updated, it just refreshes in the view with the old data. The original code came from here, I've made some minor adjustments to make it 'work': http://www.phpeasystep.com/mysql/10.html Thank you for any help Gary <?php $host="xxxx"; // Host name $username="xxxx"; // Mysql username $password="xxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="xxxx"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?> <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action=""> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Id</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>Lastname</strong></td> <td align="center"><strong>Email</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"> <?php $id[]=$rows['id']; echo $rows['id']; ?> </td> <td align="center"> <input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>"> </td> <td align="center"> <input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"> </td> <td align="center"> <input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"> </td> </tr> <?php } ?> <tr> <td colspan="4" align="center"> <input type="submit" name="Submit" value="Submit"> </td> </tr> </table> </td> </tr> </form> </table> <?php // Check if button name "Submit" is active, do this if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE $tbl_name SET name='".$name[$i]."', lastname='".$lastname[$i]."', email='".$email[$i]."' WHERE id='".$id[$i]."'"; $result1=mysql_query($sql1); } } if($result1){ header("location:update_multiple.php"); } mysql_close();
  13. Well nearly sloved, I've narrowed it down to this code snippet, for some reason the array doesnt work, when I have $text = "some text"; is works but plots the same text on each bar. //text array $text = array("A","B","C"); $red = imagecolorallocate($im, 255, 0, 0); // Now plot each column for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width; $y1 = $height-$column_height; $x2 = (($i+1)*$column_width)-$padding; $y2 = $height; imagefilledrectangle($im,$x1,$y1,$x2,$y2,$colours[$i]); imagestringup($im, 5,$x1,$x2,$text,$red);
  14. solved it myself!!!! used needed to add: imagestringup($im, 5, $y1, 190, "Hello World!", $red); // Now plot each column for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width; $y1 = $height-$column_height; $x2 = (($i+1)*$column_width)-$padding; $y2 = $height; // imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imagefilledrectangle($im,$x1,$y1,$x2,$y2,$colours[$i]); imagestringup($im, 5, $y1, 190, "Hello World!", $red);
  15. Hi again, I have a bar chart, standard 3 verticle bars coloured differently, but now i want to put some text on them, say: Bar A, Bar B, Bar C. Also this text has to be rotated 90 degrees So here is the code to make the bars, I guess I need to use: $red = imagecolorallocate($im, 255, 0, 0); imagestring($im, 5, 50, 50, "Bar A", $red); In some kind of loop so: $text = array( imagestring($im, 5, 50, 50, "Bar A", $red); imagestring($im, 5, 50, 50, "Bar B", $red); imagestring($im, 5, 50, 50, "Bar C", $red); ); can anyone help? for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width; $y1 = $height-$column_height; $x2 = (($i+1)*$column_width)-$padding; $y2 = $height; //SQL connect stuff // This array of values is just here for the example. //$values = array("5","6","7"); // Get the total number of columns we are going to plot $columns = count($values); // Get the height and width of the final image $width = 150; $height = 200; // Set the amount of space between each column $padding = 15; // Get the width of 1 column $column_width = $width / $columns ; // Generate the image variables $im = imagecreate($width,$height); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); $colours = array( imagecolorallocate ($im,0xff,0x00,0xcc), imagecolorallocate ($im,0xcc,0xff,0x00), imagecolorallocate ($im,0x00,0xcc,0xff) ); // Fill in the background of the image imagefilledrectangle($im,0,0,$width,$height,$white); $maxv = 0; // Calculate the maximum value we are going to plot for($i=0;$i<$columns;$i++)$maxv = max($values[$i],$maxv); // Now plot each column for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width; $y1 = $height-$column_height; $x2 = (($i+1)*$column_width)-$padding; $y2 = $height; // imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imagefilledrectangle($im,$x1,$y1,$x2,$y2,$colours[$i]); // This part is just for 3D effect imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } // Send the PNG header information. Replace for JPEG or GIF or whatever header ("Content-type: image/png"); imagepng($im); imagedestroy($im); ?>
  16. ah its ok, all works, it didnt save probably! thank you
  17. Thanks for that, looks exactly like what I need, although it does seem to work... have I done something silly, I have double and double checked it but I can't see whats wrong: <?php //SQL stuff // This array of values is just here for the example. //$values = array("5","6","7"); // Get the total number of columns we are going to plot $columns = count($values); // Get the height and width of the final image $width = 150; $height = 200; // Set the amount of space between each column $padding = 15; // Get the width of 1 column $column_width = $width / $columns ; // Generate the image variables $im = imagecreate($width,$height); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); $colours = array( imagecolorallocate ($im,0xff,0x00,0xcc), imagecolorallocate ($im,0xcc,0xff,0x00), imagecolorallocate ($im,0x00,0xcc,0xff) ); // Fill in the background of the image imagefilledrectangle($im,0,0,$width,$height,$white); $maxv = 0; // Calculate the maximum value we are going to plot for($i=0;$i<$columns;$i++)$maxv = max($values[$i],$maxv); // Now plot each column for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width; $y1 = $height-$column_height; $x2 = (($i+1)*$column_width)-$padding; $y2 = $height; // imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imagefilledrectangle($im,$x1,$y1,$x2,$y2,$colours[$i]); // This part is just for 3D effect imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } // Send the PNG header information. Replace for JPEG or GIF or whatever header ("Content-type: image/png"); imagepng($im); ?>
  18. hi, I hope some one can be of help? I want to make a graph using php and GD with data from MySQL, so far success. Now I want to ammend the script (see end of message) to display text on each bar, e.g red, green, blue. Also currently I loop through when setting the bar colours, in this case grey, how can I get it so there are 3 different colours? There will only ever be 3 bars. Thank you for any help Gary <?php $dbhost = 'host'; $dbuser = 'user'; $dbpass = 'password'; $dbname = 'db'; $tblname = 'tbl'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); // Make a MySQL Connection $query = sql goes here! $result = mysql_query($query) or die(mysql_error()); $values = array(); // Print out result while($row = mysql_fetch_array($result)){ array_push($values,$row['COUNT(Answer)']);} // This array of values is just here for the example. //$values = array("5","6","7"); // Get the total number of columns we are going to plot $columns = count($values); // Get the height and width of the final image $width = 150; $height = 200; // Set the amount of space between each column $padding = 15; // Get the width of 1 column $column_width = $width / $columns ; // Generate the image variables $im = imagecreate($width,$height); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); // Fill in the background of the image imagefilledrectangle($im,0,0,$width,$height,$white); $maxv = 0; // Calculate the maximum value we are going to plot for($i=0;$i<$columns;$i++)$maxv = max($values[$i],$maxv); // Now plot each column for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width; $y1 = $height-$column_height; $x2 = (($i+1)*$column_width)-$padding; $y2 = $height; imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); // imagefilledrectangle($im,$x1,$y1,$x2,$y2,$colour[$i]); // This part is just for 3D effect imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } // Send the PNG header information. Replace for JPEG or GIF or whatever header ("Content-type: image/png"); imagepng($im); ?>
  19. oh it works... I mmust have done something wrong, thank you
  20. Hi, sorry but that doesnt seem to solve my problem... I will rephrase: I have $values=array("1","3","7") I need to change it so it is something like this: $values=array(MySQL_result) Here is the simplified code from above // Make a MySQL Connection $query = "SELECT Question, COUNT(Answer) FROM question1 GROUP BY Question"; $result = mysql_query($query) or die(mysql_error()); $values = array(); // result while($row = mysql_fetch_array($result)) { array_push($values,$row['COUNT(Answer)']); }
  21. firstly i am quite rusty at php/sql so sorry in advance. I have a php script to produce graphs with the GD library, alls working well, but I want to modify it so the data comes from my database. so I connect to the database in the usuall way but the script uses an array $values = array("23","32","35","57","12","3","36","54","32","15","43","24","30"); so I need mysql to out put in the same format This is what I have so far, it prints out the data with some formating, thank you for any help you can offer Gary // Make a MySQL Connection $query = "SELECT Question, COUNT(Answer) FROM question1 GROUP BY Question"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "There are ". $row['COUNT(Answer)'] ." ". $row['Question'] ." items."; }
  22. Can I do that on the commandline? If so how? The server is in the netherlands and Im back in the UK for xmas but I have root access. Thanks
  23. Hi, Well I installed a LAMP server on ubuntu karmic from the CD, everything worked fine, then I installed mapserver, that went surprisingly well, then I restarted the server using apachectl restart, it stopped no problems but then tried to start and returned the following: * Starting web server apache2 apache2: Syntax error on line 203 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/php5.load: Cannot load /usr/lib/apache2/modules/libphp5.so into server: /usr/lib/libxml2.so.2: undefined symbol: gzopen64 [fail] how do I fix this? the libphp5.so exists and is in ../modules/ any ideas? Thankfull as always Gary
  24. Ok, I'm still having problems with deleting from my db through php code: //Delete the customer rows (only if the form has been submitted) if (isset($_POST['submit'])) { foreach ($_POST['todelete'] as $delete_id) { $query="DELETE FROM login WHERE ID = $delete_id"; mysql_query($query) or die ('Error in query: ' .mysql_errno() . mysql_error()); } echo 'Customer(s) removed.<br />'; } echo"<form>"; // Display the customer rows with checkboxes for deleting $query="SELECT * FROM login"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)){ echo '<input type="checkbox" value="' . $row['id'] .'"name="todelete[]" />'; echo $row['NAME_FIRST']; echo ' '.$row['NAME_LAST']; echo ' '.$row['EMAIL']; echo '<br />'; } echo "<input type='submit' name='submit' value='submit' />"; echo "</form>"; displays fine but does not do anything!
  25. I need it to update the database too with either yes or no, so update table name SET validate=yes where ID = $(the checked boxes) and update table name SET validate=no where ID = $(the checked boxes) also where in my code do I put input type="checkbox" name="delete" value="1" /> <?php if (sizeof($_POST) && isset($_POST['delete']) && $_POST['delete'] === 1) { ..delete entry.. } Sorry I'm an annoying newbee!
×
×
  • 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.