My dropdown list would not drop down - php

I tried to use php to create a dropdown list populated by a SELECT query but it would not dropdown. Here is my php script
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
// while($row = #mysqli_fetch_array($result))
// {
// echo "<p>$row[0] $row[1]</p>\n"; //this works
// }
echo "<td bgcolor=\"#E6E6E6\"";
echo "<strong>Category:</strong>";
echo "<select name=\"category\">";
while($row = #mysqli_fetch_array($result))
{
// echo "<p><option value='".$row[0]."'>".$row[1]."</option></p>\n";
echo "<option value='".$row[0]."'>".$row[1]."</option>";
}
mysqli_close($dbc);
echo "</select>";
echo "</td>";
?>
The query worked (the commented while loop outside the dropdown list displayed 15 records). But the dropdown list only showed one category_name and would not dropdown. Can someone help me figure out the problem? Thanks.

There are a few problems with your code.
You can either remove both echo "<td bgcolor=\"#E6E6E6\""; and echo "</td>";
which is the reason why your data is not showing.
Or, add the appropriate <table></table> tags.
Plus this echo "<td bgcolor=\"#E6E6E6\""; should be echo "<td bgcolor=\"#E6E6E6\">"; there was a missing > so that alone is breaking your <td>.
So in order to have the proper table syntax do:
Sidenote: I added . "\n" in order to get clean and well-aligned HTML source.
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
echo "<table>" . "\n";
echo "<tr>" . "\n";
echo "<td bgcolor=\"#E6E6E6\">" . "\n";
echo "<strong>Category:</strong>" . "\n";
echo "<select name=\"category\">" . "\n";
while($row= mysqli_fetch_array($result)){
echo "<option value='".$row[0]."'>".$row[1]."</option>" . "\n";
}
echo "</select>" . "\n";
echo "</td>" . "\n";
echo "</tr>" . "\n";
echo "</table>" . "\n";
mysqli_close($dbc);
?>
Edit: (Try this) since I don't know what your full code looks like.
<form id="form1" name="form1" method="post" action="form1_handler.php">
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
if(isset($_POST['submit'])){
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
echo "<table>" . "\n";
echo "<tr>" . "\n";
echo "<td bgcolor=\"#E6E6E6\">" . "\n";
echo "<strong>Category:</strong>" . "\n";
echo "<select name=\"category\">" . "\n";
while($row= mysqli_fetch_array($result)){
echo "<option value='".$row[0]."'>".$row[1]."</option>" . "\n";
}
echo "</select>" . "\n";
echo "</td>" . "\n";
echo "</tr>" . "\n";
echo "</table>" . "\n";
mysqli_close($dbc);
} // brace for if(isset($_POST['submit']))
?>
<input type="submit" name="submit" value="Submit">
</form>

Related

call php function to put data in a table

i have a function called displayMenu() that brings data from a table .. this function is inside a php file named functions.php
if (isset($_POST['Starters'])) {
$sql = "SELECT * FROM product WHERE category_id=1";
$sql1= "SELECT cat_name,cat_description FROM category WHERE cat_id=1";
displayMenu();
}
function displayMenu(){
global $db, $sql,$sql1,$nb;
if($result = mysqli_query($db, $sql1)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo '<h1 class="header">' . $row['cat_name'] . '</h2>';
echo '<h3 class="content">' . $row['cat_description'] . '</h3>';
}
}
}
if($result = mysqli_query($db, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['p_code'] . "</td>";
echo "<td>" . $row['p_name'] . "</td>";
echo "<td>" . $row['p_description'] . "</td>";
echo "<td>" . $row['p_price'] . "</td>";
echo "<td>" ;
echo '<form class="nbItem" action="order.php" method="post">Nb of items:';
echo '<input type="number" name="item"> <button type="submit" class="btn" name="addtocart">Add To Cart</button>';
echo '</form>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
this actually works .. but i want to create the table inside another file called order.php and call this function from this file
how should i do it ?
If I understand correctly, you just need to include your functions.php file in your order.php file and then call the displayMenu from includes your order file?
order.php
include 'functions.php';
echo displayMenu();

How to display two distinct MySQL tables on same PHP page

I have two sql tables I'm reading from and I would like to display each table as its own, separate table. First the internal, and then the external. And I'd like to have a simple title prefacing each one. For instance:
INTERNAL
Table here
EXTERNAL
Table here
What's happening though is the INTERNAL and EXTERNAL titles keep appearing
on the same line, or both come before the tables. Like this:
INTERNAL
EXTERNAL
Internal Table here
External Table here
I've tried including the titles as part of the php tags. Those are the currently commented out lines. And I've also tried adding the html outside of the php tags like this:
<br><br>
<b>INTERNAL</b>
<br><br>
<?php Internal Table here ?>
And then:
<br><br>
<b>EXTERNAL</b>
<br><br>
<?php External Table here ?>
But that still results in both titles appearing first, before the tables. Does the html get processed prior to the php? I must be interpreting this too linearly, like a script because it's certainly not being processed in the order in which it is coded on my page.
This is my code as is, which has the titles commented out. The tables appear sandwiched together which tells me they are not being interpreted as two distinct elements on the page. Do I need to put each one in it's own tag?
<?php
$internal = getInternalNetworkTable();
if ($internal->num_rows > 0){
//echo " <b>INTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $internal->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
} else {
echo "No results founds";
}
?>
<?php
$external = getExternalNetworkTable();
if ($external->num_rows > 0){
//echo " <b>EXTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $external->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
} else {
echo "No results founds";
}
$conn->close();
?>
EDIT:
Needed to add:
echo "</table>";
New code:
<?php
$internal = getNovatoInternalNetworkTable();
if ($internal->num_rows > 0){
echo " <b>INTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $internal->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
echo "</table>";
} else {
echo "No results founds";
}
?>
<?php
$external = getNovatoExternalNetworkTable();
if ($external->num_rows > 0){
echo " <b>EXTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $external->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
echo "</table>";
} else {
echo "No results founds";
}
$conn->close();
?>
You're missing </table> in both display of tables.
Therefore your table will be merge in each other.
echo '</table>';
Before both of your } else { should do the trick

Php Listing Data with datatables

I have a datatable and i am listing 3 columns from database.(Actually got 25)
I have added a submit button for each row and want to see the all data when user click that button on the bottom of the page.
Here is my code:
<?php
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM jobs";
if($result = mysqli_query($db, $sql)){
if(mysqli_num_rows($result) > 0){
while($row =
mysqli_fetch_array($result)){
echo "<td><button type='submit' value=".$row['id']".">Show Details</a></td>";
echo "<td>'" . $row['type'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row["result"] . "</td>";
}
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No data.</em></p>";
}
} else{
echo "ERROR: Could not able to execute
$sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($db);
?>
I tried to use Post method but didn't worked.
you can use jquery to do it like this.
<?php
require_once 'config.php';
// Attempt select query execution
if(!mysqli_select_db($conn,$db))
die("Cant select to database");
$sql = "SELECT * FROM jobs";
if($result = mysqli_query($db, $sql)){
echo "<table id='example'>";
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><button type='submit' value=". $row['id'] . ">Show Details</a></td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row["result"] . "</td>";
echo "</tr>";
}
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No data.</em></p>";
}
echo "</table>";
} else{
echo "ERROR: Could not able to execute
$sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($db);
?>
<script>
$('#example').on('click', 'tbody .edit_btn', function () {
var $row = $(this).closest("tr"), // Finds the closest row <tr>
$tds = $row.find("td"); // Finds all children <td> elements
$.each($tds, function() { // Visits every single <td> element
console.log($(this).text()); // Prints out the text within the <td>
});
});
</script>

I need to add a dropdown to a table cell

I need to add two dropdown lists (from a Mysql query) to a table. I have the table that shows the results of r aquery and then I need to have the cells of the two columns with the dropdowns in them. This is going to be a form that will eventually create files.
Here is my code so far:
<?php
$link = mysql_connect("localhost", "", "") or die ('Error connecting to mysql' . mysql_error());
mysql_select_db("cqadmin");
$sql = "SELECT id , mac FROM phones order by mac;";
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT id , templatename FROM templates order by templatename;";
$result1 = mysql_query($sql1) or die(mysql_error());
$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
<tr>
<th>Extension #</th>
<th>Secret</th>
<th>MAC Address</th>
<th>Template</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";
echo "<td>" . $row['secret'] . "</td>";
echo "<td>" . $row[''] . "</td>";
echo "<td>" . $row[''] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<p>
<select name="phone">
<?php
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . '">' . $row['mac'] . '</option>';
}
?>
</select>
<select name="template">
<?php
while($row = mysql_fetch_array($result1)) {
echo '<option value="' . $row['id'] . '">' . $row['templatename'] . '</option>';
}
?>
</select>
</p>
<?php
mysql_close($link);
?>
I have tried to insert the select into the row but the page doesn't load I get an server error.
Any help is much appreciated
Ok, try this:
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
$link = mysql_connect("localhost", "", "") or die ('Error connecting to mysql' . mysql_error());
mysql_select_db("cqadmin");
$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
<tr>
<th>Extension #</th>
<th>Secret</th>
<th>MAC Address</th>
<th>Template</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
$sql = "SELECT id , mac FROM phones order by mac;";
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT id , templatename FROM templates order by templatename;";
$result1 = mysql_query($sql1) or die(mysql_error());
echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";
echo "<td>" . $row['secret'] . "</td>";
echo "<td> <select name='phone'>";
while($rowA = mysql_fetch_array($result)) {
echo '<option value="' . $rowA['id'] . '">' . $rowA['mac'] . '</option>';
}
echo "</select></td>";
echo "<td><select name='template'>";
while($rowB = mysql_fetch_array($result1)) {
echo '<option value="' . $rowB['id'] . '">' . $rowB['templatename'] . '</option>';
}
echo "</select></td>";
echo "</tr>";
}
echo "</table>";
?>
If you get any errors put them here. Keep in mind that the code above is preety bad, you shouldn't code like that. It's a good start to learn how things work, but later you should refactor it. Write it better, more readable, write your own functions getting data from db, use mysqli functions instead mysql or even library like PDO, separate your logic from the view etc.

How do I filter a MySQL query with a select option which results in the right product

I'm working on an CMS which is based on an database of course.
The structure of the database is as follow (the language of this is Dutch):
id, artikelnummer_fabrikant, breedte, diepte, hoogte, extrainfo, afmeting, kleur, merk, serie, producttype, omschrijving, prijsfinal, levertijd
With the next piece of code I get all the information out of the database and place it in a table:
(Sorry that I dont use < and > to create the tags, this text editor sees it as tag and not as text and I have no idea how to make this work correct, anny tips on this will be welcome!)
function get_content() {
echo "<p>";
echo "<table>";
echo "<tr>";
echo "<td> id </td>";
echo "<td> artikelnummer frabrikant </td>"
and so on
echo "</tr>";
$sql = "SELECT * FROM products";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) != 0) :
while ($row = mysql_fetch_assoc($res)) :
echo "<tr>";
echo "<td> . $row['id'] . </td>";
echo "<td> . $row['artikelnummer_fabrikant'] . </td>";
and so on
echo "</tr>";
endwhile;
echo "</table>";
echo "</p>";
endif;
Now I would like to build a filter on this with the tags select and option so that when I select the option brand(merk) 'windsor' it will show all products with the brand 'windsor' with all there information such as breedte, hoogte, diepte (width, heigt, depth).
I got a piece of code which gives me all the brands(merk) in an option menu:
function category_filter() {
echo "Merk: ";
echo "<select onchange=window.location = this.value>;";
$sql = "SELECT merk FROM products";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) !=0 ) :
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) :
echo "<option> . $row['merk'] . </option>";
endwhile;
endif;
echo "</select>";
And of cource I have 1 for each item in the database.
What I want to do next is combine these 2 codes in some kind of way that when i select the brand(merk) 'Windsor' in the option menu brand(merk) it will show all products with the brand 'Windsor' + all the other information of that product.
I'm struggeling for a week trying to make this happend and I can't get it going like the way i want it to go :(.
How can i make this work?
Ok so i found out how to do this by writing the following code:
echo "<form class='merk' action='index.php'>";
echo "<select name=merk>";
echo "<option value=''>Kies een merk</option>";
while ($row_merk = mysqli_fetch_array($merk_query_selected)) :
$filter = $row_merk['merk'];
if($filter == $merk) :
echo "<option value=" . urlencode($filter) . " selected='selected'>" . $filter . "</option>";
else :
echo "<option value=" . urlencode($filter) . ">" . $filter . "</option>";
endif;
endwhile;
echo "</select>";
echo "<input class='input-merk' type='submit' value='Filter op merk'>";
echo "</form><br />";
$('form').each(function() {
var that = $(this);
$.post(that.attr('action'), that.serialize());
});</script>
Have it decoded:
$merk = urldecode($_GET["merk"]);
And then get the contents by running this while loop:
while ($row_merk = mysqli_fetch_array($merk_query_all)) :
echo $table_row;
echo $table_cell, $row_merk['id'] . $table_div_end;
echo $table_cell, $row_merk['artikelnummer_fabrikant'] . $table_div_end;
echo $table_cell, $row_merk['breedte'] . $table_div_end;
echo $table_cell, $row_merk['diepte'] . $table_div_end;
echo $table_cell, $row_merk['hoogte'] . $table_div_end;
echo $table_cell, $row_merk['extrainfo'] . $table_div_end;
echo $table_cell, $row_merk['afmeting'] . $table_div_end;
echo $table_cell, $row_merk['kleur'] . $table_div_end;
echo $table_cell, $row_merk['merk'] . $table_div_end;
echo $table_cell, $row_merk['serie'] . $table_div_end;
echo $table_cell, $row_merk['producttype'] . $table_div_end;
echo $table_cell, $row_merk['omschrijving'] . $table_div_end;
echo $table_cell, $row_merk['prijsfinal'] . $table_div_end;
echo $table_cellEnd, $row_merk['levertijd'] . $table_div_end;
echo $table_row_end;
endwhile;
Now what i wanna do is make it dynamic so that it can filter on multiple selections at the same time.
Doing my best, anny help would be welcome!
[EDIT] Found out how this works, but its still not dynamic. But this question has been awnsered, I will go and look on the web how to make this dynamic.

Categories