English is not my first language so i'm trying to be as specific as possible.
I have a table stored , all of the signed up account to my website. Inside of it i have a dropdown box automatically generated by php. So the problem is, when i click to changed the option in the dropdown menu, all of my option appeared outside of the box and was duplicated . Just like this error dropdown box. Here is the code
$sql1="Select ID, user_login,user_email, display_name from wp_users ";
$result1=mysqli_query($conn,$sql1);
echo '<div id="div_package" style="display: none" >';
echo '<div style="text-align:center"><h1>Đổi gói thành viên</h1><input type="text" id="myInput_pack" onkeyup="FilterUsers()" placeholder="Nhập thông tin người dùng cần tìm" title="Type in a user" style="width: 20%;height:35px; font-size: 16px;border: 1px solid black; margin-bottom: 12px;text-align:center"></div>';
echo "<table id='tb_user' class='tbadmin' border='1' cellpadding='10' align='center' style='border-collapse:collapse'>";
echo "<tr><th>ID người dùng</th><th>Tên đăng nhập</th><th>Tên hiển thị</th><th>Email</th><th>Gói hiện tại</th><th></th></tr>";
while($row= mysqli_fetch_array($result1))
{
$userid=$row['ID'];
echo '<tr>';
echo '<td>'.$userid.'</td>';
echo '<td>'.$row['user_login'].'</td>';
echo '<td>'.$row['display_name'].'</td>';
echo '<td>'.$row['user_email'].'</td>';
echo "<td><select name='package_$userid' id='package'>";
$sqlcheck="SELECT 1 FROM wp_usermeta WHERE user_id=".$userid." AND meta_key='package_id' LIMIT 1";
$result_check= mysqli_query($conn,$sqlcheck);
if( mysqli_num_rows($result_check)!=0)
{
$sql_metaval="Select meta_value from wp_usermeta where user_id=".$userid." and meta_key='package_id'";
$result_metaval= mysqli_query($conn,$sql_metaval);
while($metaval= mysqli_fetch_array($result_metaval))
{
$val_package=$metaval['meta_value'];
$test="Select post_title, ID from wp_posts where post_type='membership_package' and ID=".$val_package ;
$sql2="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>".$val_package ;
$result_test=mysqli_query($conn,$test);
if(mysqli_num_rows($result_test)==0)
{
echo '<option value="">FREE</option>';
$sql3="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>''";
$packages= mysqli_query($conn,$sql3);
while($pack = mysqli_fetch_assoc($packages)){
echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
}
echo '</select>';
}
else
{
while($row= mysqli_fetch_array($result_test))
{
echo '<option value="'.$val_package.'">'.$row['post_title'].'</option>';
}
$packages= mysqli_query($conn,$sql2);
while($pack = mysqli_fetch_assoc($packages)){
echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
}
echo '<option value="">FREE</option>';
echo '</select>';
}
}
}
else
{
echo '<option value="">FREE</option>';
$sql3="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>''";
$packages= mysqli_query($conn,$sql3);
while($pack = mysqli_fetch_assoc($packages)){
echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
}
echo '</select>';
}
echo "<td><a onclick='return confirmEdit();' href='#' id='update_$userid' class='update_user'>Cập nhật</a></td>";
echo '</tr>';
}
echo "</table></br></div>";
It's appear that some kind of loop has happened in the first else section. I tried to delete the select tag in side that section, all the option appeared inside but still be duplicated like this one
Can someone show me how to solve the duplicate option? This has trouble me for days.
I think the problem because you loop the selected option and not selected options in same select without exclude the select option from query of get not selected options ($sql2).
$test="Select post_title, ID from wp_posts where post_type='membership_package' and ID=".$val_package ;
$sql2="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>".$val_package ;
-------------------------------------------------------
while($row= mysqli_fetch_array($result_test))
{
echo '<option value="'.$val_package.'">'.$row['post_title'].'</option>';
}
$packages= mysqli_query($conn,$sql2);
while($pack = mysqli_fetch_assoc($packages)){
echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
}
Related
Here is the function I am working on. I need to get the selected option which is inside a while loop.
Every time I am trying to get the selected value through select id, it's only getting the last selected value for the rest of the row.
Please help me. How can I get and store the full table data?
function generate_schedule($con,$sy,$year_id){
$time = array(1=>'9:30-11:00',2=>'11:30-1:00',3=>'1:30-3:00',4=>'3:15-4:45');
$days = array(1=>'Sunday',2=>'Monday',3=>'Tuesday',4=>'Wednesday',5=>'Thursday',6=>'Friday',7=>'Saturday');
$rooms=array(1=>'302',2=>'303',3=>'304',4=>'701',5=>'702',6=>'704',7=>'901',8=>'902',9=>'1402',10=>'1405');
$yls=mysqli_query($con,"SELECT * FROM year_level_subject as yls join subjects as s on yls.subj_id=s.subj_id where yls.year_id='$year_id'");
$num=mysqli_num_rows($yls);
$x = 0;
echo '<table class="table table-bordered table-hover" id=" datatable">';
echo '<thead><tr><th>Time</th><th>Days</th><th>Subject</th><th>Teacher</th><th>Room</th></tr></thead>';
while($row=mysqli_fetch_assoc($yls)){
extract($row);
echo '<tr>';
echo '<td><select id ="seltime" ><option value=0>Select Time</option>';
foreach($time as $key => $value) {
echo '<option value='.$key.'>';
echo $value .'</option>';
}
echo '</select></td>';
echo '<td><select class="selday" ><option value=0>Select Day</option>';
foreach($days as $key => $value) {
echo '<option value='.$key.'>';
echo $value .'</option>';
}
echo '</select></td>';
echo '<td>'.$subj_desc.'</td>';
$teach = mysqli_query($con,"SELECT * FROM teachers");
echo '<td><select id="selteacher" onchange="schedata(this)"><option value=0>Select Teacher</option>';
while($row=mysqli_fetch_assoc($teach)){
extract($row);
echo '<option value='.$teach_id.'>'.$teach_fname.' '.$teach_mname.' '.$teach_lname.'</option>';
}
echo '</select></td>';
echo '</select></td>';
echo '<td><select name ="selroom" ><option value=0>Select Room</option>';
foreach($rooms as $key => $value) {
echo '<option value='.$key.'>';
echo $value .'</option>';
}
echo '</select></td>';
echo '</tr>';
}
echo '</table>';
}
you did't give the name atribute to your select element
echo '<td><select id="selteacher" onchange="schedata(this)" >
give a name for this as
echo '<td><select id="selteacher" name="selteacher" onchange="schedata(this)" >
I have an SQL-database with many tables. Now I would like to create an input-form to be able to get data into the db without writing the entire sql-code every time. And this should work as follows:
All table names are listed in a drop-down menu. After having selected a table name, a new table with 4 columns is created automatically:
The first column of this table simply contains an increasing number.
The second column contains the field-names of the selected table.
In the third column there are empty input fields to enter the values for the database. Only in the third line (=product name) there is a drop-down menu with all product names from the main-table of the db.
The fourth column contains the data type (e.g. int or varchar)
All tables in the database have the same structure in the first 3 columns: the first column contains the table-id, the second column the foreign-key (=master_id) and the third column the product_name.
Up to this point, the script works well with the following 2 php-files (javasql.php and getuser.php):
javasql.php:
enter code here
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="" class="optdrugs">please select</option>
<?php
include("files/zugriff.inc.php"); // database Access
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'product'";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value="'. $row['TABLE_NAME'] . '">' .
$row['TABLE_NAME']. '</option>';
echo '<br>';
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Bitte Tabelle auswählen:</b>
<br>
<?php
if (isset($_POST["submit"])) {
$sent = $_POST['sent'];
$q = $_POST['tablename'];
$column_passed = unserialize($_POST['column']); // content of array
$column is passed from getuser.php
foreach ($_POST["insertvalue"] as $key => $value) {
echo $value . "<br>";
$werte[] = "'$value'";
}
$sql="INSERT INTO $q ($column_passed) VALUES (" .
implode(", ", $werte) . ")"; // data entry
mysqli_query($db, $sql);
if (mysqli_affected_rows($db) > 0) {
echo "<h3 style='color:blue'>successful</h3>";
} else {
echo "<h3 style='color:red'>not
successful</h3>";
}
}
?>
</div>
</body>
</html>
enter code here
getuser.php:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<form id="formdatabase" name="formdatabase" action="javasql.php"
method="post">
<input type="hidden" name="sent" value="yes">
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','product');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM $q";
$result = mysqli_query($con,$sql);
$numcols = mysqli_num_fields($result); // gets number of columns in result table
$field = mysqli_fetch_fields($result); // gets the column names from the result table
$data_type_array = array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
252=>'text',
253=>'varchar',
254=>'char',
246=>'decimal'
);
$data_type_array = array_flip($data_type_array);
echo "<table>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th><th>" . 'Column names' . "</th>
<th>" . 'Values for db-entry' . "</th><th>" . 'Type' . "</th>";
echo "</tr>";
echo "<tr>";
$nr = 1;
for($x=0;$x<$numcols;$x++):?>
<td><?= $nr; ?></td>
<td><?= $field[$x]->name; ?></td>
<?= $column[] = $field[$x]->name; ?>
<td>
<?php
if ($field[$x]->name == 'Name') { // if-Beginn
?>
<select name="insertvalue[<?= $x; ?>]" id="insertvalue<?=
$x; ?>" size="1" onchange = "javascript:getSelectedRow()">
<?php
include("files/zugriff.inc.php");
$sql = "SELECT * FROM product_main ORDER BY Name";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value='. $row['Name'] . '>' .
$row['Name'] . '</option>';
echo '<br>';
}
?>
</select>
<?php
$name_option = "";
} else {
$name_option = "<input type='text' id='insertvalue" . $x . "'
name='insertvalue[" . $x . "]' size='50'>";
echo $name_option;
}
?>
</td>
<?php
$key = array_search($field[$x]->type, $data_type_array);
if($key !== false){
echo "<td>" . $key . "</td>";
}else{
echo "<td>" . $field[$x]->type . "</td>";
}
?>
<td><?= $field[$x]->type; ?></td>
<?= $nr = $nr + 1; ?>
</tr>
<?php endfor;
echo "</table>";
mysqli_close($con);
?>
<input type="hidden" name="tablename" value="<?= $q; ?>">
<input type="hidden" name="column" value="<?php echo htmlentities
(serialize($column)); ?>">
<input type="submit" value="Enter values" name="submit">
</form>
</body>
</html>
Since I need the master_id (= foreign key) in addition to the product-name for database entry, I would like to extend my script, so that the respective master_id is automatically sent to the input field in line 2, when a product-name is selected in line 3 ... without clicking a button. I tried to do this with javascript but it didn´t work. As far as I know, the solution would be to use AJAX but unfortunately, I am not very used to AJAX.
I would be more than happy, if someone could help me to solve this problem!
So i am trying to display data in sql server using PHP. I am using a sortable library i found online and this is my code:
echo "<table id='report' class='sortable'>";
echo "<thead><tr>";
echo "<th>A</th>";
echo "<th>B</th>";
echo "<th>C</th>";
echo "<th>D</th>";
echo "<th>E</th>";
echo "<th>F</th>";
echo "<th>G</th>";
echo "<th>H</th>";
echo "<th>I</th>";
echo "</tr></thead>";
echo "<tbody>";
for($i=0; $i<$tablerows; $i++)
{
echo "<tr>";
echo "<td>".$result[$i]['A']."</td>";
echo "<td>".$result[$i]['B']."</td>";
echo "<td>".$result[$i]['C']."</td>";
echo "<td>".$result[$i]['D']."</td>";
echo "<td>".$result[$i]['E']."</td>";
echo "<td>".$result[$i]['F']."</td>";
echo "<td>".$result[$i]['G']."</td>";
echo "<td>".$result[$i]['H']."</td>";
echo "<td>".$result[$i]['I']."</td>";
echo "<td><div class='arrow'></div></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='5'>";
echo "<div id='nestedAccordion'>";
echo "<h2>COLUMN 1 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "<h2>COLUMN 2 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "<h2>COLUMN 3 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "<h2>COLUMN 4 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "<h2>COLUMN 5 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "</div>";
echo "</td>";
echo "</tr>";
When my page is loaded, it can be sorted by the A-I headings in <th> tag. As you can see, in my 'for' loop my last tag has an arrow. I want that so if the user wants more information about the row, he can view that by clicking that arrow, and information that will be displayed is in: <td colspan='5'>
Here is the javascript/jquery:
$(document).ready(function () {
$("#report tr:odd").addClass("odd");
$("#report tr:not(.odd)").hide();
$("#report tr:first-child").show();
$(".arrow").click(function(){
$(this).parents("tr").next("tr").toggle();
$(this).toggleClass("up");
});
var parentDivs = $('#nestedAccordion div'),
childDivs = $('#nestedAccordion h3').siblings('div');
$('#nestedAccordion h2').click(function () {
parentDivs.slideUp();
if ($(this).next().is(':hidden')) {
$(this).next().slideDown();
} else {
$(this).next().slideUp();
}
});
$('#nestedAccordion h3').click(function () {
childDivs.slideUp();
if ($(this).next().is(':hidden')) {
$(this).next().slideDown();
} else {
$(this).next().slideUp();
}
});
});
The question and the problem i have is only either sortable for table or arrow will work. When i load the page, and i click on the arrow to view more information i can view it, but if i sort afterward, the data under the arrow is taken as its own row rather than part of the row it was selected from.
If i sort at start, and then click on arrow, the row below is hidden. I think the problem is in this chunk of code:
$(".arrow").click(function(){
$(this).parents("tr").next("tr").toggle();
$(this).toggleClass("up");
});
Since the next tr is for colspan? But how do i fix it?
Here i am collecting the category and area value from the search-form using get method. So as i m having 7 records into the database and i want to display only 4 records per page and when user clicks on Next button then the remaining records need to get displayed but here my code is displaying only first four records from the database but when i click on next button then the remaining 3 records are not getting displayed.
Consider area=Mumbai and category=lawn. and if there is only 7 records into the database then i want first four to get display in first page and remaining 3 into the same page on clicking the next button..
Here is my code:
<?php
$link=mysql_connect("localhost","root","");
mysql_select_db("celebration_db",$link);
$q="select count(*) \"total\" from hallinfo";
$ros=mysql_query($q,$link);
$row=(mysql_fetch_array($ros));
$total=$row['total'];
$dis=4;
$total_page=ceil($total/$dis);
$page_cur=(isset($_GET['page']))?$_GET['page']:1;
$k=($page_cur-1)*$dis;
$category = $_GET['category'];
$area = $_GET['area'];
//$category = 'hall';
//$area = 'nagpur';
echo $category;
echo $area;
$q="select * from hallinfo where category = '$category' AND area = '$area' limit $k,$dis";
//$q="select * from hallinfo limit $k,$dis";
$ros=mysql_query($q,$link);
while($row=mysql_fetch_array($ros))
{
$imageId = $row['id'];
echo '<div id="pattern">';
echo '<table>';
echo '<tr>';
echo '<td colspan="2" width="300px" height="200px">
<img src="image-show.php?id='.$imageId.'">';
echo '</td>';
echo '<td width="150px" height="200px">'.$row['description'];
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.$row['rent'];
echo '</td>';
echo '<td>'.$row['capacity'];
echo '</td>';
echo '<td>'.$row['area'];
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
}
//echo '</table>';
//echo '<br/>';
if($page_cur>1)
{
echo '';
}
else
{
echo '<input style="background-color:green;border:1px black solid;border-radius:5px;width:120px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value=" Previous ">';
}
for($i=1;$i<$total_page;$i++)
{
if($page_cur==$i)
{
echo ' <input style="background-color:green;border:2px black solid;border-radius:5px;width:30px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value="'.$i.'"> ';
}
else
{
echo ' <input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:30px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value="'.$i.'"> ';
}
}
if($page_cur<$total_page)
{
echo '<input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:90px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value=" Next ">';
}
else
{
echo '<input style="background-color:green;border:1px black solid;border-radius:5px;width:90px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value=" Next ">';
}
?>
It would seem your primary issue is in your count. While you are selecting only specific rows (in the above example rows with category = .. and area=.., you're initial count of the rows is returning the count of ALL rows in the database. So while there may be 7 rows in total, perhaps there are only 4 rows with the given filter.
As an aside, you should really be using mysqli: http://www.php.net/mysqli
This might be a stupid question but I would like to have a clarification on how to go about this. I have come across quite a few articles that let to set the values of a text box depending on the choices made in a drop down menu using jQuery and Ajax. My issue is when I'm trying to do the same depending on the choices made in 5 drop down menus. I have the Id values stored in the database which should be used to populate the text box. Can anyone guide on how to go about this issue with multiple drop downs. This is my code so far:
<?php
$sql1="SELECT Schlungen FROM schulung as s";
$result=mysql_query($sql1);
echo "<p align='left'> <strong>Schulung 1</strong> <select name='Schlungen1'> <option value default></option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . " </option>";
}
echo "</select>";
?>
<?php
error_reporting(0);
//Drop Down for Schulung 2
$sql2="SELECT Schlungen FROM schulung as s";
$result=mysql_query($sql2);
echo "<p align='left'> <strong>Schulung 2</strong> <select name='Schlungen2'> <option value default></option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . " </option>";
}
echo "</select>";
?>
<?php
error_reporting(0);
//Drop Down for Schulung 3
$sql3="SELECT Schlungen FROM schulung as s";
$result=mysql_query($sql3);
echo "<p align='left'> <strong>Schulung 3</strong> <select name='Schlungen3'> <option value default></option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . "</option>";
}
echo "</select>";
?>
<?php
error_reporting(0);
//Drop Down for Schulung 4
$sql4="SELECT Schlungen FROM schulung as s";
$result=mysql_query($sql4);
echo "<p align='left'> <strong>Schulung 4</strong> <select name='Schlungen4'><option value default></option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . " </option>";
}
echo "</select>";
?>
<?php
error_reporting(0);
//Drop Down for Schulung 5
$sql5="SELECT Schlungen FROM schulung as s";
$result=mysql_query($sql5);
echo "<p align='left'> <strong>Schulung 5</strong> <select name='Schlungen5'><option value default></option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . " </option>";
}
echo "</select>";
?>
<p align="left"><strong>Access_level </strong>
<input type="text" name="a_level" disabled="disabled">
</p>
For achieving auto completion,you can create a select field in html file,call a javascript function on keyup event and use jQuery for calling your php file
<html>
<head>
<script>
$('.autosuggest').keyup(function(){
$.post("<your file.php>",{any data you need},function(data){
//echo the data
//echo "<option value='" . $row['Schlungen'] . "'>" . //$row['Schlungen'] ." </option>";
$('.result').html(data)
});
});
$('.result option').click(function(){
var rValue = $(this).text();
$('.autosuggest').attr('value',rValue);
$('.result').html('');
});
</script>
</head>
<body>
<input type='text' class='autosuggest'/>
<select class='result'>
</select>
</body>
</html>
try to set the value of each html select option to be the id of that text
so the echo should be
echo "<option value='" . $row['id'] . "'>" . $row['Schlungen'] . " </option>";
then on the html select add the event onchange and add the JS function foo() to fill a hidden input with all selected ids by getting them from the page using the JQuery or Javascript
also you have to add id or class for each html select to get its selected value easily
$("#select1").val();
$("#select2").val(); ...
then insert these values in a hidden input field so the hidden input now contains all selected ids