showing my database tables using selection in php - javascript

i'm trying to display all my tables in database with this code.
<label>
<select class="selectpicker" name="tabel">
<?php
include "koneksi.php";
$koneksi ->select_db('testskripsi');
$sql = mysqli_query($koneksi, "show tables") ;
while($table = $sql->fetch_array(MYSQLI_ASSOC)) {
echo '<option name="opsi" value = "' . $table['Tables'] . '">' . $table['Tables'] . '</option>';
}
?>
</select>
</label>
here's my result
i think i don't know how to fill this array to make my tables displayed
' . $table['Tables'] . '

Please try Tables_in_testskripsi instead of Tables.here is the correct code of yours.
<label>
<select class="selectpicker" name="tabel">
<?php
include "koneksi.php";
$koneksi ->select_db('testskripsi');
$sql = mysqli_query($koneksi, "show tables") ;
while($table = $sql->fetch_array(MYSQLI_ASSOC)) {
echo '<option name="opsi" value = "' . $table['Tables_in_testskripsi'] . '">' . $table['Tables_in_testskripsi'] . '</option>';
}
?>
</select>
</label>
do let me know if it was helpful for you

To get table names,
$tables=$this->db->query("
SELECT t.TABLE_NAME AS myTables
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE t.TABLE_SCHEMA = 'database name'
AND t.TABLE_NAME LIKE '%a%'
")->result_array();
foreach($tables as $key => $val) {
echo $val['myTables']."<br>";// myTables is the alias used in query.
}

Related

Select2 Dropdown options disappear once I select any option

<select rows='3' name="user_id[]" required="required" multiple class="form-control select2" style="width: 100% !important">
<?php
$qid = $_GET['id'];
$student = $conn->query('SELECT u.*,s.course as ls, s.branch, s.year FROM users u left join students s on u.id = s.user_id where u.user_type = 3 ');
while ($row = $student->fetch_assoc()) {
$user_id = $row['id'];
$result = $conn->query('SELECT * FROM quiz_student_list where quiz_id ="'.$qid.'" and user_id = "'.$user_id.'"');
if ($result->num_rows == 0) {
?>
<option value="<?php echo $row['id'] ?>"><?php echo ucwords($row['name']) . ' ' . $row['ls'] . ' ' . $row['branch'] . ' ' . $row['year'] ?></option>
<?php }
}
?>
</select>
$(".select2").select2({
placeholder: "Select here",
width: 'resolve'
});
I have a students dropdown in select2 jquery but suppose I click on any one of the name it got selected but the options disappears. I have to click again to see the students list and click again on any other name. I want to select multiple in one go only.
$(".select2").select2({
placeholder: "Select here",
width: 'resolve',
closeOnSelect: false
});
closeOnSelect: false
by using this property I have actually got what I was looking for. I have set it to false so that it doesn't get closed on selection of any one of the options.

how can we access value passed to a function under the select tag(profession) outside the function call

I have a function named as "getSelectValue(this.value)" that gets executed on changing the select options.I want to somehow use this value inside the next select tag.is it possible?
if($res = $mysqli->query($sql))
{
echo '
<select id="profession" name="profession" onchange="getSelectValue(this.value);">';
$f1=1;
echo'<option selected="true" disabled="disabled">Filter by Profession</option> ';
while ($row = $res->fetch_assoc()) {
echo "<option value='" . $row['Designation'] ."'>" . $row['Designation'] ."</option>";
}
echo '</select>';
$res->free();
}
$ids = "SELECT DISTINCT `Identifies As` FROM `therapists`";
if($res = $mysqli->query($ids))
{
if($f1==0)
{
echo '<select id="idas" name="idas" onchange="getSelectValue(this.value);">
<option selected="true" disabled="disabled">Identifies as</option> ';
while ($row = $res->fetch_assoc()) {
echo "<option value='" . $row['Identifies As'] ."'>" . $row['Identifies As'] ."</option>";
}
echo '</select>';
$res->free();
}
else{
//this.value to be used here
}
}
I want to access the this.value(under profession id) at the else part of the next select tag.

when update the table in mysql through dropdown, all the row also updated

I try to do a web page that contains a dropdown.
When dropdown is selected, I want to update the mark into MySQL database based on Enum, but the code does not work.
I use javascript with PHP to query into MySQL.
<form id="myForm" method="post" onsubmit="return submitform()">
<select id="lvl" name="lvl" style="height:30px;">
<option value="std1"selected="selected">
<?php echo $stu1name["Stu_name"] ?>
</option>
<option value="std2" >
<?php echo $stu2name["Stu_name"] ?>
</option>
<option value="std3" >
<?php echo $stu3name["Stu_name"] ?>
</option>
<option value="std4" >
<?php echo $stu4name["Stu_name"] ?>
</option>
<option value="std5" >
<?php echo $stu5name["Stu_name"] ?>
</option>
</select>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
This is my javascript with PHP:
function submitform() {
var option= document.getElementById('lvl').value;
if (option == "std1"){
?php
mysqli_query($conn,
"UPDATE evaluation set mid_mark='" . $_POST["mid_mark"] .
"',end_mark='" . $_POST["end_mark"] .
"', performance='" . $_POST["performance"] .
"' WHERE Enum ='1'"
);
?>
return true;
}
if (option == "std2"){
<?php
mysqli_query($conn,
"UPDATE evaluation set mid_mark='" . $_POST["mid_mark"] .
"',end_mark='" . $_POST["end_mark"] .
"', performance='" . $_POST["performance"] .
"' WHERE Enum ='2'"
);
?>
return true;
}
if (option == "std3"){
<?php
mysqli_query($conn,
"UPDATE evaluation set mid_mark='" . $_POST["mid_mark"] .
"',end_mark='" . $_POST["end_mark"] .
"', performance='" . $_POST["performance"] .
"' WHERE Enum ='3'"
);
?>
return true;
}
if (option == "std4"){
<?php
mysqli_query($conn,
"UPDATE evaluation set mid_mark='" . $_POST["mid_mark"] .
"',end_mark='" . $_POST["end_mark"] .
"', performance='" . $_POST["performance"] .
"' WHERE Enum ='4'"
);
?>
return true;
}
if (option == "std5"){
<?php
mysqli_query($conn,
"UPDATE evaluation set mid_mark='" . $_POST["mid_mark"] .
"',end_mark='" . $_POST["end_mark"] .
"', performance='" . $_POST["performance"] .
"' WHERE Enum ='5'"
);
?>
return true;
}
}
but when I update, all the row is updated like this
image
I don't know where I'm doing wrong here. I'm completely lost here.
This is because php run in the server and all code run before your page is loading, so you can't control it by script in the browser like javascript. You can insert data in tour database without javascript with this php code put at the top of your page. You have to delete the onsubmit statement in your form.
<?php
if (isset($_POST['lvl'])) {
$enum = substr($_POST['lvl'], -1, 1);
$sql = "UPDATE evaluation SET mid_mark=:mid_mark, end_mark=:end_mark, performance=:performance WHERE Enum='$enum'";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':mid_mark', $_POST['mid_mark'], PDO::PARAM_STR);
$stmt->bindParam(':end_mark', $_POST['end_mark'], PDO::PARAM_STR);
$stmt->bindParam(':performance', $_POST['performance'], PDO::PARAM_STR);
$stmt->execute();
}
?>

Only getting last record from mysql database

I am stuck on a code where I want to fetch data from MySQL into an array.. I have a form containing color and size select boxes, an onclick javascript function is triggered and it created two more select boxes like above, I have managed to get data into the javascript code where the code for creating new select boxes is written.
But I am only getting the last inserted record from both the tables. Although I have used a while loop.
Can someone help me out and I am also getting name of all select boxes likes name="color[]" , I want to insert records into a bridge table containing ids of color and size. below is my code please help ..
I will clear it up , each time I click add more button it should create 2 new dropdown lists, one for color and 2nd for size, both dropdowns should have the distinct data from database. so the ids for each record would be same in every dropdown list, I want to add more than 1 records in bridge table which contains product_id,color_id and size_id, so if I go for 3 dropwdown boxes , and i select blue color and small size in the first, then for second dropdown i again select blue color and size medium, as for the last dropdown which was also generated by the javascript function . i choose black color and large size. so from the dropdown it will get ids of size,color and it would be inserted accordingly.. so when i display the product and color blue is selected i would only see the sizes which were added to the color blue at the time of adding the product.. i hope this clears everything :)
$result=mysql_query("SELECT * FROM color,size");
while($row=mysql_fetch_array($result)) {
?>
<script>
var room = 1;
function add_fields() {
room++;
var objTo = document.getElementById('room_fileds')
var divtest = document.createElement("div");
divtest.innerHTML = '<div class="label">Room ' + room + ':</div><div class="content"><span>Color: <select name="color[]"><option value="<?php echo $row['color_id']; ?>"><?php echo $row['color']; ?></option></select></span><span>Size: <select><option value="<?php echo $row['size_id']; ?>"><?php echo $row['size']; ?></option></select></span></div>';
objTo.appendChild(divtest)
}
</script>
<?php
}
HTML code
<div id="room_fileds">
<div>
<div class='label'></div>
<div class="content">
<input type="button" class="btn btn-success" id="more_fields" onclick="add_fields();" value="Add More" /> <br /><br />
<select name="color[]" class="form-control">
<option value="0">Select Color</option>
<?php
$result=mysql_query("SELECT * FROM color");
while($row=mysql_fetch_array($result)){
?>
<option value="<?php echo $row['color_id'] ?>"><?php echo $row['color']; ?></option>
<?php } ?>
</select>
<select name="size[]" class="form-control">
<option value="0">Select Size</option>
<?php
$result=mysql_query("SELECT * FROM size");
while($row=mysql_fetch_array($result)){
?>
<option value="<?php echo $row['size_id'] ?>"><?php echo $row['size']; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
Replace your first code with the following:
<script>
var colors = [];
var sizes = [];
var room = 1;
<?php
$result = mysql_query("SELECT * FROM color");
while ($row = mysql_fetch_array($result)) { ?>
colors.push(['<?php echo $row['color_id'] ?>', '<?php echo $row['color'] ?>']);
<?php }
$result = mysql_query("SELECT * FROM size");
while ($row = mysql_fetch_array($result)) { ?>
sizes.push(['<?php echo $row['size_id'] ?>', '<?php echo $row['size'] ?>']);
<?php } ?>
function add_fields() {
room++;
var objTo = document.getElementById('room_fileds');
var divtest = document.createElement("div");
var html = '<div class="label">Room ' + room + ':</div><div class="content"><span>Color: <select name="color[]" class="form-control">';
for (i = 0; i < colors.length; i++) {
html += '<option value="' + colors[i][0] + '">' + colors[i][1] + '</option>';
}
html += '</select></span><span>Size: <select name="size[]" class="form-control">';
for (i = 0; i < sizes.length; i++) {
html += '<option value="' + sizes[i][0] + '">' + sizes[i][1] + '</option>';
}
html += '</select></span></div>';
divtest.innerHTML = html;
objTo.appendChild(divtest);
room++;
}
</script>
$divs='';
$result=mysql_query("SELECT * FROM color,size");
$room=1;
while($row=mysql_fetch_array($result)) {
$divs.='<div><div class="label">Room ' . $room . ':</div><div class="content"><span>Color: <select name="color[]"><option value="'.$row['color_id'].'">'.$row['color'].'</option></select></span><span>Size: <select><option value="'.$row['size_id'].'">'.$row['size'].'</option></select></span></div></div>';
$room++;
} ?>
<script>
function add_fields() {
var objTo = document.getElementById('room_fileds');
objTo.appendChild(<?php echo $divs; ?>);
}
</script>
Try this code hope it works for you
<script>
var room = 1;
function add_fields() {
room++;
var objTo = document.getElementById('room_fileds');
var divtest = document.createElement("div");
<?php
$div_start='<div class="label">Room '."'+ room +'".' :</div><div class="content">';
$color='<span>Color: <select name="color[]">';
$size='<span>Size: <select>'
$result=mysql_query("SELECT * FROM color,size");
while($row=mysql_fetch_array($result)) {
$color.='<option value="'.$row['color_id'].'">'.$row['color'].'</option>';
$size.='<option value="'.$row['size_id'].'">'.$row['size']'.</option>';
}
$color.='</select> </span>';
$size.='</select> </span>';
$div_close='</div>';
$innerHTML=$div_start.$color.$size.$div_close;
?>
divtest.innerHTML ='<?php echo $innerHTML; ?>';
objTo.appendChild(divtest);
}
</script>
This is the final code , it would help if anyone wants to code a similar thing.. just change values and it would work as per your need... and i would like to thank all people who replied to the thread and specially Mohammad Anini .. it wouldnt have been possible without his help !!!
$query = mysql_query("INSERT INTO products (product_name,product_description, product_pic1, product_pic2, product_pic3,product_price,category_id,subcategory_id,product_status,color,size,product_slug, meta_keywords,entrydate)
VALUES ('$product', '$description' , '$image', '$image2', '$image3', '$product_price', '$category', '$subcategory', '$product_status', '$capture_field_vals', '$size', '$product_slug1', '$meta_keywords', Now() )") or die(mysql_error());
if ($query === TRUE) {
$lastid = mysql_insert_id();
$color["color"]=array();
$size["size"]=array();
foreach ($_POST['color'] as $key => $colorvalue) {
}
foreach ($_POST['size'] as $key => $sizevalue) {
}
$cid=array();
foreach($color as $rec){
$cid[]=$rec;
}
$sz=array();
foreach($size as $rec){
$sz[]=$rec;
}
$length=sizeof($color);
for($i=0;$i<$length-1;$i++){
$query2 = mysql_query("INSERT INTO bridge (product_id,color_id, size_id)
VALUES ('$lastid', '$cid[$i]' , '$sz[$i]')") or die(mysql_error());
// echo "<script>alert('New Product successfully Addedd');windows.location.replace('addedit_product.php');</script>";
}

PHP Database Query: How to return the results from a while loop to a Javascript Variable?

I have a php query like so:
<?php
$query = "SELECT * FROM " . $usertable . " ORDER BY fname;";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo '<option value="' . $row['pkid'] . '">' . $row['fname'] . ' ' . $row['lname'] . '</option>';
}
?>
Within this same .php file I have some javascript. All I would like to do is return $row['fname'] as a a javascript variable.
Is this possible?
If you want to output the PHP variable in a JavaScript variable you could do something like this -
echo '<script>var name+' . $row['pkid'] . ' = ' .$row['fname'] . ';</script>';
This would give you a uniquely named variable for each row.

Categories

Resources