I'm trying to create a dropdown list with all the values that i have inside my table, but I can't do it, the select box stay blank.
My PHP code, I'm calling a function
Job category
<select>
<?php
function category($category_id, $category_name){
echo "<option value='".$category_name."'>".$category_name."</option>";
}
?>
</select>
And my function
function selectcategory(){
connect();
$category = ("SELECT * FROM job_categories");
$val = DB_array($category, 'a+');
$ii = count($val);
$ii = $ii - 1;
for ($i = 0; $i <= $ii; $i++) {
$category_id = $val[$i]['category_id'];
$category_name = $val[$i]['category_name'];
category($category_id, $category_name);
}
}
Assuming the flow of your code , that 2nd part executes first , i.e. connection is made , data is fetched and you are calling category() , it will execute
function category($category_id, $category_name){
echo "<option value='".$category_name."'>".$category_name."</option>";
}
$ii times and you are returning to the calling function .
So that's why < select > does not populate the data because it is interpreted after running the above code.
<select>
<?php
$result = category($category_id, $category_name);
foreach($result as $key => $value){
echo "<option value='".$key."'>".$value."</option>";
}
?>
</select>
$sql = mysql_query("SELECT * FROM job_categories");
$result = mysql_db_query($dbname, $sql) or die("Failed Query of " . $sql);
while ($row = mysql_fetch_row($result)){
echo "<option value='".$row["category_id"]."'>".$row["category_name"]."</option>"; // the value should be id
}
Related
Here, i'am had some case that i thought that are uniq for me.
this the problem, i want to looping some tag
3 times. The loop is work, but when it was input to database...
just 1 loop that worked.
Here the Screenshot,
The value of loop
Result of loop
<td>
<label for="baris">Baris</label>
<select name="baris">
<option value="">--</option>
<?php
for ($i2=A; $i2 < F; $i2++) {
echo "<option name='".$i2."'>".$i2."</option>";
}
?>
</select>
</td>
<td>
<label for="kolom">Kolom</label>
<select name="kolom">
<?php
for ($i3=1; $i3 <= 10; $i3++) {
echo '<option name="'.$i3.'">'.$i3.'</option>';
}
?>
</select>
</td>
also this code for input to database
<?php
if(isset($_POST['masukan'])){
$nama = htmlspecialchars($_POST['nama']);
$email = htmlspecialchars($_POST['email']);
$jk = htmlspecialchars($_POST['jk']);
$notlp = htmlspecialchars($_POST['notlp']);
$queryinput = mysqli_query($link, "UPDATE tb_customer
SET nama='$nama',
email='$email',
notlp='$notlp',
jk='$jk'
WHERE id_cust='$id_cust1'
");
$querytampil = mysqli_query($link, "SELECT * FROM tb_customer WHERE id_cust ORDER by id_cust desc limit 1");
$dcus = mysqli_fetch_assoc($querytampil);
$id_cust = $dcus['id_cust'];
$querytampil2 = mysqli_query($link, "SELECT max(num_ticket)AS num FROM tb_ticket");
$dnumti = mysqli_fetch_assoc($querytampil2);
$num_ticket = $dnumti['num'];
$hasil = $num_ticket + 1;
for ($i5= 1; $i5 <= $p; $i5++) {
$baris = htmlspecialchars($_POST['baris']);
$kolom = htmlspecialchars($_POST['kolom']);
if($queryinput){
$id_flight = $data['id_flight'];
$queryinput2 = mysqli_query($link, "INSERT INTO tb_ticket VALUES('','$id_cust','$id_flight','$hasil','','','','','$id_dest','$id_ori', '$baris', '$kolom')");
if ($queryinput2) {
$querytampil3 = mysqli_query($link, "SELECT * FROM tb_ticket order by num_ticket desc limit 1");
$dtick = mysqli_fetch_assoc($querytampil3);
$nt = $dtick['num_ticket'];
echo "<script>alert('Succes.')</script>";
echo '<script>window.location="pembayaran.php?num_ticket='.$nt.'&&id_cust='.$id_cust.'"</script>';
}else{
echo "<script>alert('Your data cannot send.')</script>";
}
}else{
echo "<script>alert('Your data cannot send, please check your input data.')</script>";
}
}
}
?>
Bad engslish sorry*
If I got this correct; it's because you are not setting your html variables as an array.
The first changes you need to make are within your view page..
<select name="baris"> -> <select name="baris[]">
<select name="kolom"> -> <select name="kolom[]">
This sets your $_POST['baris'] and $_POST['kolom'] as arrays.
Since you're already looping...
for ($i5= 1; $i5 <= $p; $i5++) {
$baris = htmlspecialchars($_POST['baris']);
$kolom = htmlspecialchars($_POST['kolom']);
This stuff now needs to access the relevant array... And becomes...
for ($i5= 1; $i5 <= $p; $i5++) {
$baris = htmlspecialchars($_POST['baris'][$i5]);
$kolom = htmlspecialchars($_POST['kolom'][$i5]);
I new to php and mysql.
I wrote the function below:
function catOption() {
$maincatfunc_query = mysql_query("SELECT * FROM mainCats ORDER BY id ") or die(mysql_error());
$funcCat = array();
while ($mainCatFunc = mysql_fetch_array($maincatfunc_query)) {
for ($i = 0; $i < count($maincatfunc_query); $i++) {
$funcCat[$i] = '<option value="'.$maincatfunc_sorgu['mainCatID'].'">' . $maincatfunc_sorgu['name'] . '</option>';
}
}
for ($i = 0; $i < count($maincatfunc_query); $i++) {
return $funcCat[$i];
}
}
I want to fetch "all" value from mysql database and fill it in a dropdown. So i wrote a function like this. But it does not work.
And i don't think count() function doesn't really work in this conditions. How can i get the max count of mysql array ?.
or besides can i do this without using a function ?. I've googled it for a long time but i can't find any usefull info.
Thanks !
You should use like below: [returns array]
function catOption()
{
$query = mysql_query("SELECT * FROM mainCats ORDER BY id ") or die(mysql_error());
$arrCat = array();
while ($row = mysql_fetch_array($query)) {
$arrCat[] = '<option value="'. $row['mainCatID'].'">'. $row['name'].'</option>';
}
return $arrCat;
}
Or this one [returns string]
function catOption()
{
$query = mysql_query("SELECT * FROM mainCats ORDER BY id ") or die(mysql_error());
$arrCat = "";
while ($row = mysql_fetch_array($query)) {
$arrCat .= '<option value="'. $row['mainCatID'].'">'. $row['name'].'</option>';
}
return $arrCat;
}
You should try this,
function catOption() {
$maincatfunc_query = mysql_query("SELECT * FROM mainCats ORDER BY id ") or die(mysql_error());
while ($mainCatFunc = mysql_fetch_array($maincatfunc_query)) {
$funcCat .= '<option value="'.$mainCatFunc['mainCatID'].'">' . $mainCatFunc['name'] . '</option>';
}
return $funcCat;
}
Use like this :
$dropDownData = catOption();
put it into HTML
<select><?php echo $dropDownData; ?></select>
I tried many things but i just cant make the value of option an id from database and i cant write the option as date and title from database
im doing this so far, any help would be appreciated.
<select name="agenda "size="10">
<?php
global $connection;
$result = mysql_query("SELECT * FROM agenda where date > now() order by date", $connection);
$i = 0;
while ($row = mysql_fetch_array($result) && $i < 20)
{
$id = $row['id_agenda'];
$date = $row['date'];
$title = $row['title'];
//here i would like to make an option with
//value = id_agenda and write the date_agenda and title_agenda
//something like this
//<option value="$row[$id]">$date $title</option>
$i++;
}
?>
<option value="Google">meeting 2</option>
</select>
Use:
echo "<option value=\"$id\">$date $title</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[id_agenda]\">$row[date] $row[title]</option>";
}
I have a table named members with 3 rows. The following code attempts to display all the rows in the members table. It displays the 1'st record 3 times instead of displaying each record once.
<?php
$con = new mysqli("localhost", "root", "jce123", "profile");
if (mysqli_connect_errno()) {
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
$keysarray = array_keys($array);
$valuearray = array_values($array);
for ($i=0; $i < $num; $i++) {
for($j = 0; $j < count($array); $j++) {
echo "<table>";
echo "<tr><td>".$keysarray[$j].": </td><td>".$valuearray[$j]."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
mysqli_close($con);
?>
I've updated this per suggestions:
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
while ($row = mysqli_fetch_assoc($res)) {
foreach($array as $key => $value){
echo "<table>";
echo "<tr><td>".$key.": </td><td>".$value."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
That's because, your $num = 3, count($array) = 3 and the outer for loop has no bearing on inner for loop.
Where you have $array = mysqli_fetch_assoc($res);, you will only receive one row from your database. You need something like:
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
}
Any basic tutorial will tell you this. Even the PHP docs provide an example.
you can use do something like this..
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
e.g
echo $row['id'];
echo $row['name'];
etc
}
try this... this peace of code is not tested but just to give you an idea...
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
echo "<table>";
echo '<tr><td> id = "'.$row['id'].'":
</td><td> name = "'.$row['name'].'"</td></tr>";
echo "</table>";
}
How to make <option selected="selected"> set by MySQL and PHP?
My code:
echo '<select>';
$tempholder = array();
$rs = mysql_query("SELECT * FROM id ORDER BY year");
$nr = mysql_num_rows($rs);
for ($i=0; $i<$nr; $i++){
$r = mysql_fetch_array($rs);
//if($year==$r["year"]){ $selected=' selected="selected"'; }//doesn't work so
if (!in_array($r['year'], $tempholder)){
$tempholder[$i] = $r['year'];
echo "<option>".$r["year"]."</option>";//<option$selected>...
}
}
unset($tempholder);
echo '</select>';
Try this one:
echo '<select>';
$tempholder = array();
$rs = mysql_query("SELECT * FROM id ORDER BY year");
$nr = mysql_num_rows($rs);
for ($i=0; $i<$nr; $i++){
$r = mysql_fetch_array($rs);
if (!in_array($r['year'], $tempholder)){
$tempholder[$i] = $r['year'];
echo "<option".(($year==$r["year"])? ' selected="selected"' : '').">".$r["year"]."</option>";
}
}
unset($tempholder);
echo '</select>';
It doesn't saves the state in a variable which you have to overwrite.
And I think the real error was the single equal sign in $year=$r["year"] and not wihtin the rest of the code.
In addition to fixing the =/== gotcha, you can save yourself the array lookup and make the code simpler by asking the database to return each year only once in the query:
<select>
<?php $result= mysql_query('SELECT DISTINCT year FROM id ORDER BY year'); ?>
<?php while($row= mysql_fetch_assoc($result)) { ?>
<option <?php if ($row['year']==$year) { ?>selected="selected"<?php } ?>>
<?php echo htmlspecialchars($row['year']); ?>
</option>
<?php } ?>
</select>
(You may not need htmlspecialchars() assuming that's a numeric year, but it's good practice always to HTML-escape any plain text you include in an HTML template. You can define a function with a shorter name to do the echo htmlspecialchars to cut down on typing.
)
You must define $selected everytime, and you were using the assignment operator instead of the comparison:
echo '<select>';
$tempholder = array();
$rs = mysql_query("SELECT * FROM id ORDER BY year");
$nr = mysql_num_rows($rs);
for ($i = 0; $i < $nr; $i++){
if($year == $r["year"]) { //not $year = $r["year"]
$selected=' selected="selected"';
}
else {
$selected = "";
}
$r = mysql_fetch_array($rs);
if (!in_array($r['year'], $tempholder)){
$tempholder[$i] = $r['year'];
echo "<option$selected>" . $r["year"] . "</option>";
}
}
unset($tempholder);
echo '</select>';
Adding a new answer here for posterity, since the old code, which while correct at the time (actually mysqli did exist, but many hosts didn't support PHP 5), is unfortunately using deprecated code. Instead of using mysql_ extensions, here's a way to handle it using an object oriented approach which will work with mysqli_ connections:
Here's the database connection
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Assuming that the $year variable is coming from a form (though it could be used from GET or SESSION or wherever)
$year = $_POST['year'];
Here's the query for the option button (I have it broken out into different rows to make it a little easier to read):
$result=$conn->query($sql);
while($row = $result->fetch_assoc()) {
if ($row['year']==$year) {
$selected = 'selected="selected"';
}
else {
$selected = '';
}
echo '<option value="'.$row['year'].'" '. $selected . '>"'
. $row['year'] .'</option>';
}