Jump to content

Gazz1982

Members
  • Posts

    83
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

Gazz1982's Achievements

Member

Member (2/5)

0

Reputation

1

Community Answers

  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); ?>
×
×
  • 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.