I am taking info from a database query and adding it to a dropdown menu form (that is placed inside a table). The query is in a separate function that is called from within the form. It adds the info from the database to the correct location on the table, but it is not in the dropdown menu. I used variables $a, $b, and $c to test my syntax, and it works fine with those variables. Is it an issue with the function call? Any ideas?
Here is the code:
<?php
function fill_dropdown(){
include("../secure/database.php");
$conn = pg_connect(HOST." ".DBNAME." ".USERNAME." ".PASSWORD)
or die('Could not connect: ' . pg_last_error()); //error if could not connect to database
$query = "SELECT country_code, name FROM lab5.country ORDER BY name ASC";
$result = pg_query($query) or die("Unable to execute: " . pg_last_error($conn));
$numRow = 0;
//results are good so output them to HTML
//echo "test<br />";
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
//echo "test<br />";
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 0){
$code[$numRow] = $col_value;//array($numRow => $col_value);
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$code[$numRow]\" />";
//echo $code[$numRow] . "<br />";
}
elseif($counter == 1){
$country_name[$numRow] = $col_value;
echo "<option value=$country_name[$numRow]>$country_name[$numRow]</option>";
//echo $country_name[$numRow] . "<br />";
}
$counter++;
}
$numRow++;
}
//echo "end test<br />";
}
echo "<table border = \"1\">";
echo "<form method=\"POST\" action=\"exec.php\">"; //save and cancel buttons
for($i=1; $i<5; $i++) //building initial table
{
echo "\t<tr>\n";
echo "\t\t<td>";
if($i == 1)
echo "Name";
elseif($i == 2)
echo "Country Code";
elseif($i == 3)
echo "District";
else
echo "Population";
echo "</td>\n";
echo "<td>\n";
if($i == 1){
echo "<input type=\"text\" name=\"name\">";
}
elseif($i == 2){
echo "<select name=\"country_code\">"; //dropdown box
$c = 0; //these are just to show that this way works
$a = "IOT";
$b = "test2";
$numRow = 1;
echo "<option value=\"IOT\">British Indian Ocean Territory</option>";
echo "<option value=$a>$b</option>";
//echo "<option value=" . $country_name[$numRow] . ">" . $country_name[$numRow] . "</option>";
fill_dropdown();
//echo "<option value=\"Brunei\">Brunei</option>";
echo "</select>";
}
elseif($i == 3){
echo "<input type=\"text\" name=\"district\">";
}
else{
echo "<input type=\"text\" name=\"population\">";
}
}
echo "</td>";
echo "</tr>";
echo "</table>";
echo "\t\t<input type=\"submit\" value=\"Save\" name=\"save\" />";
echo "<input type=\"button\" value=\"Cancel\" onclick=\"top.location.href='" . $_SERVER['HTTP_REFERER'] . "';\" />\n";
echo "</form>";
?>
It would seem you can replace
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
//echo "test<br />";
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 0){
$code[$numRow] = $col_value;//array($numRow => $col_value);
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$code[$numRow]\" />";
//echo $code[$numRow] . "<br />";
}
elseif($counter == 1){
$country_name[$numRow] = $col_value;
echo "<option value=$country_name[$numRow]>$country_name[$numRow]</option>";
//echo $country_name[$numRow] . "<br />";
}
$counter++;
}
$numRow++;
}
with
while ($row = pg_fetch_assoc($result)) {
// why do you want this line at all?
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$row[country_code]\"/>";
echo "<option value=\"$row[name]\">$row[name]</option>";
}
The only thing I can see that is wrong, is not having quotes around the option's value attribute. I don't understand what you are expecting to achieve by interleaving hidden inputs with options, though. Judging by your $a, $b, $c template, maybe what you actually want is:
while ($row = pg_fetch_assoc($result)) {
echo "<option value=\"$row[country_code]\">$row[name]</option>";
}
Related
I have been trying to work this out for a while.
I have created a form with a dropdown box that gets results from a database. from this i then $_POST that from to another page. From that second page i wish to get the ID number and then get the records and display them on screen.
I will then put them in a table to organise the results better.
can anyone help me in achieving this.
Here is the code for the form (which works and sends the $PlantID)
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
/* $plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];*/
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
I have created this page to get the ID and display that ID on screen. AS you can tell i have probably doubled up on ways to try work this out.
$sql = "SELECT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
if(isset($_POST['submit'])){
$selected_val = $_POST['Botannical_Name']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
echo "<br />";
echo "well:".$_POST["Botannical_Name"]."<br/>";
echo "now:".$plantquery."<br />";
echo $_POST;
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
Any help would be greatly appreciated.
you should use following to get the selected value,
$selected_val = $_POST['flower'];
if(isset($_POST['submit'])){
$selected_val = $_POST['flower']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
$sql = "SELECT * FROM PLANTS WHERE FlowerID='.$selected_val.'";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while ($row=mysqli_fetch_assoc($result))
{
echo $row['Botannical_Name'];
}
}
echo "<br />";
print_r($_POST);
if(!empty(_POST)) {
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
Database:
Calendar 1:
Calendar 2:
Calendar 3:
I'm trying to getting both events from the database onto Monday 4th and I'm wondering why I am only getting one event for April and May.
$sql = "SELECT title, contact, contact_email, DAYOFMONTH(start_date)
FROM $caltbl WHERE start_date LIKE '$year-$month%'";
$result = mysql_query($sql)or die(mysql_error());
$row_count = mysql_num_rows($result);
$record = mysql_fetch_array($result);
while ($day_num <= $days_in_month) {
echo "<td width=\"42\" valign=\"top\">
<a href='index.php?day=$day_num-$title-$year'>
$day_num</a><p/>";
if ($day_num == $record['DAYOFMONTH(start_date)']){
echo " ".$record['title']. "<br/>";
} else{
echo "<br /> " . "<br/> ";
}
echo "</td>";
$day_num++;
$day_count++;
EDIT:
while ($day_num <= $days_in_month) {
echo "<td width=\"42\" valign=\"top\"> <a href='index.php?day=$day_num-$title-$year'>$day_num</a><p/>";
if ($day_num == $record['DAYOFMONTH(start_date)']){
while ($event = mysql_fetch_assoc($result)) {
$array[] = $event;
}
print_r($array);
echo " ".$event['title']. "<br/>";
} else{
echo "<br /> " . "<br/> ";
}
echo "</td>";
$day_num++;
$day_count++;
// Make sure we start a new row each week
if ($day_count > 7) {
echo "</tr><tr>";
$day_count = 1;
}
}
Open a blank array:
$arr = array();
while ($event = mysql_fetch_assoc($result)) {
$arr[] = $event;//store all the data in the $arr array
}
Run a foreach loop to to print the event detail:
foreach($arr as $k=>$v){
if($v['DATEOFMONTH(start_date)']==$day_num){
echo ''.$v['title'].'<br/>';
}else{
echo "<br /> " . "<br/> ";
}
}
Hope this may help. demo
i am using jquery table to show table on a web page.The table is coming up properly but paging for the table and showing number of records and search options are not working for the table generated.All the records are populating at the load of the page.This is my code
PHP code
$qry = " SELECT AssetId,";
$qry .= $data;
$qry .= " from Completedetails";
mysql_select_db($database_finalkms, $finalkms);
$query_getcolumns = $qry;
$getcolumns = mysql_query($query_getcolumns, $finalkms) or die(mysql_error());
$row_getcolumns = mysql_fetch_assoc($getcolumns);
$totalRows_getcolumns = mysql_num_rows($getcolumns);
if (($getcolumns)||(mysql_errno == 0))
{
echo "<table width='50%' id='sample_2'><thead><tr>";
if (mysql_num_rows($getcolumns)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
//display the data
while ($rows = mysql_fetch_array($getcolumns,MYSQL_ASSOC))
{
echo "<tbody><tr >";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
Java Script
<script>
jQuery(document).ready(function() {
// initiate layout and plugins
$("#sample_2").dataTable({"bPaginate": true});
});
</script>
<input name="hdnfld" id="hdnfld" type="hidden" value="<?php echo $qry;?>"/>
Please help me in this regard.
Try the following modified code of yours, if still not working then give the live URl where ur html output is displyed.
if (($getcolumns)||(mysql_errno == 0))
{
// Displying the headers
$i = 0;
echo "<table width='50%' id='sample_2'><thead><tr>";
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
// Data Section
echo "<tbody>";
if (mysql_num_rows($getcolumns)>0)
{
while ($rows = mysql_fetch_array($getcolumns, MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
echo "</tr>";
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}
i just want to have random password from all the query results i did of the students. on my situation, i tried to generate but it returns only 1 generated password to all queried students(about 45 students). how should i make a random one. plss help..
heres my code
<?php
if(isset($_POST['generate'])){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()_+';
$generated_pass = substr(str_shuffle($charset), 0, 12);
$generate = $_POST['generate'];
}
?>
<form method="post" action='enroll_student.php' name ='register'>
<?php
$yr = date("Y");
if ($result = $mysqli->query("SELECT
tbl_studentreg.studId,
tbl_studentreg.fname,
tbl_studentreg.lname,
tbl_studentreg.mname,
tbl_studentreg.dob,
tbl_studentreg.address,
tbl_department.departmentName,
tbl_studentreg.sy
FROM tbl_studentreg
Inner Join tbl_department ON tbl_studentreg.departmentId = tbl_department.departmentId WHERE tbl_studentreg.sy = '$yr' "))
{
if ($result->num_rows > 0)
{
echo "<table width= '1000'>";
echo "<tr><th>StudentID</th><th>Name</th><th>Date of Birth</th><th>Address</th><th>Department</th><th>School Year</th><th>Password</th></tr>";
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td align='center'>" . $row->studId . "</td>";
echo "<td align='center'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
echo "<td align='center'>".$row->dob."</td>";
echo "<td align='center'>" . $row->address. "</td>";
echo "<td align='center'>".$row->departmentName."</td>";
echo "<td align='center'>".$row->sy."</td>";
if(isset($generated_pass)) {
//how could i make this one generate random password for every students..?
for($i=0; $i <= $row->studId; $i++){
echo "<td>$generated_pass</td>";
}
}
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No Results.";
}
}
else
{
echo "Error: " . $mysqli->error;
}
$mysqli->close();
echo '<br>';
include 'count.php'; //this one will give the total no. of results, just ignore.
?>
<br />
<tr><td></td></tr><tr><td><input type='submit' name='generate' value='Generate'/></td></tr>
</table>
</form>
function genpass(){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()_+';
return substr(str_shuffle($charset), 0, 12);
}
// INSIDE THE LOOP
$generated_pass = genpass();
echo "<td>$generated_pass</td>";
Something like that.
i have this code which i use to print some fields from the database.
My problem is that i get this error about foreach invalid argument supplied and a mysql fetch array problem.
The code is this:
foreach( $checked1 as $key => $value){
echo "<th> $value </th>";
}
echo "</tr></thead>";
while($row = mysql_fetch_array($result)){
Where $checked1 is an array
$checked1 = $_POST['checkbox'];
What's the problem here?
The whole code:
<?php
echo "<div id='table-3'>";
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked1 = $_POST['checkbox'];
$checked = implode(',', $_POST['checkbox']);
}
$con = mysql_connect('localhost','user','passwd');
mysql_query("SET NAMES UTF8");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con);
$result = mysql_query("SELECT $checked FROM hostess");
echo "<table >";
echo "<thead><tr>";
if(is_array($_POST['checkbox'])){
foreach( $checked1 as $key => $value){
echo "<th> $value </th>";
}
echo "</tr></thead>";
} else {
echo "Checkbox is not an array.";
}
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($value == 'photo'){
echo "<td> <img src=foto/photo1/".$row[$value] . "></td>";
} else if($value == 'photo2'){
echo "<td><img src=foto/photo2/".$row[$value] . "></td></td>";
}
else if( $value == 'photo2' && $value == 'photo'){
echo "<td> <img src=foto/photo1/".$row[$value] . "></td>";
echo "<td><img src=foto/photo2/".$row[$value] . "></td></td>";
}
else{
echo "<td>" . $row[$value] . "</td>";
}
}echo "</tr>";
}
echo "</table>";
Either $_POST['checkbox'] is not defined (no checkbox checked while sending form) or is not an array (input name does not contain [] in the end);
You should always check if variable contain what you want before performing any operation on it, ex:
if(is_array($checked1)){
foreach( ... ){
}
}
$result = mysql_query(' ... ');
if(!$result){
die(mysql_error());
}
What is your $_POST['checkbox'] called in your HTML form? It should be something like:
<input type="checkbox" name="checkbox[]" value="1" />
Check to make sure your checkbox values are coming through as arrays. Do the following:
if(is_array($_POST['checkbox'])){
// contiue with foreach...
} else {
echo "Checkbox is not an array.";
}