update with combo box - php

I have job insert page which is working fine.
I just build an update page in admin panel.
I'm not able to show screen short :(
I need to show the category as selected which is already in the job table.
let me show you the table details.This is the job table
CREATE TABLE IF NOT EXISTS `jobs` (
`job_id` int(11) NOT NULL AUTO_INCREMENT,
`job_title` varchar(99) NOT NULL,
`job_category` int(3) NOT NULL,
`job_location` varchar(33) NOT NULL,
`job_country` varchar(33) NOT NULL,
`job_salary` int(12) NOT NULL,
`job_reference` varchar(9) NOT NULL,
`job_contact_name` varchar(9) NOT NULL,
`job_description` text NOT NULL,
`job_requirments` text NOT NULL,
`job_companydetails` text NOT NULL,
`status` int(2) NOT NULL,
`date` date NOT NULL,
`featured` int(1) NOT NULL,
PRIMARY KEY (`job_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1091 ;
and this is the category table
CREATE TABLE IF NOT EXISTS `job_category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(25) NOT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
I'm just showing data from job table for update.
but the thing is that I need to show JOB CATEGORY in the Combobox.
I think it is easy through like this
<select name="job_category">
<?php
$result = mysql_query("SELECT * FROM $category_tbl");
while($row = mysql_fetch_array($result))
{
echo "<option value=$row[category_id]> $row[category_name] </option>";
}
?>
</select>
but the thing is that I need to show selected category name which is in the job table

In the while loop you can have something like:
while($row = mysql_fetch_array($result)) {
echo "<option value='$row[category_id]'";
if($row['category_id'] === $rows['job_category']){
echo "selected='selected'";
}
echo "> $row[category_name] </option>";
}
job['job_category'] would contain the category id of the job you are currently displaying.
Hope this helps.

It is has some bug
I just fixed it
look the changes
$result = mysql_query("SELECT * FROM $category_tbl");
while($row = mysql_fetch_array($result))
{
echo "<option value='$row[category_id]'";
if($row['category_id'] === $rows['job_category'])
{
echo "selected='selected'";
}
echo "> $row[category_name] </option>";
}
thanks for your clue

Related

php only shows two entries from db

I have the following mysql table:
CREATE TABLE IF NOT EXISTS `spieler` (
`spielerID` int(11) NOT NULL,
`name` varchar(50) COLLATE latin1_german1_ci NOT NULL,
`vorname` varchar(50) COLLATE latin1_german1_ci NOT NULL,
`jahrgang` varchar(10) COLLATE latin1_german1_ci NOT NULL,
`bemerkung` varchar(300) COLLATE latin1_german1_ci NOT NULL,
`mannschaft` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;
ALTER TABLE `spieler`
ADD PRIMARY KEY (`spielerID`),
ADD KEY `mannschaft` (`mannschaft`);
ALTER TABLE `spieler`
MODIFY `spielerID` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `spieler`
ADD CONSTRAINT `spieler_ibfk_1` FOREIGN KEY (`mannschaft`) REFERENCES `mannschaft` (`mannschaftID`);
and the php code to get the data out of the db:
$id = $_GET['id'];
//echo "ID ist".$id;
$sql = "SELECT * FROM spieler WHERE mannschaft = '.$id.'";
if (!$result = $db->query($sql)) {
die("Fehler: ['.$db->error.']");
}
echo "<table class='table table-striped'>";
echo "<tr><td><b>ID</b></td><td><b>Name</b></td><td><b>Vorname</b></td><td><b>Jahrgang</b></td><td><b>Bemerkung</b></td><td><b>Bearbeiten</b></td><td><b>Löschen</b></td></tr>";
while ($row = $result->fetch_assoc()) {
echo '<tr><td>'.$row['spielerID'] . '</td><td>'.$row['name'].'</td><td>'.$row['vorname'].'</td><td>'.$row['jahrgang'].'</td><td>'.$row['bemerkung'].'</td><td><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></td><td><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></td></tr>';
}
echo "</table>";
The above code works perfectly fine if there are only two entries in the table. But if there are more then two entries, the table is not getting displayed. It only shows the tablehead.
What am I doing wrong? Or am i missing something?
Change your sql query like give below.
$sql = "SELECT * FROM spieler WHERE mannschaft = '".$id."'";

get checked checkboxes from database

I am looknig to seek a way to get the tick boxes checked if they are assigned to the category in the database.
<?php
try{
// Selecting entire row from cat_list table
$results = $dbh->query("SELECT cat_id, cat_title FROM cat_list");
}catch(Exception $e) {
echo $e->getMessage();
die();
}
$category = $results->fetchAll(PDO::FETCH_ASSOC);
?>
<br>
<label><input type="checkbox" name="" class="selectall"/> Select all</label>
<div id="checkboxlist" >
<?php
foreach($category as $cat){
?>
<input type="checkbox" value="<?php echo $cat["cat_id"]; ?>" <?php echo ($cat['cat_id'] == 1) ? 'checked="checked"' : ''; ?> name="cat_no[]" id="box1"> <?php echo $cat["cat_title"]; ?></a><br>
<?php
}
So when I create the post I select from the available categories which are displayed as an array, the code above is taken from my edit post form so I want it retrieve the categories I assigned to it and tick the boxes.
I have 3 tables:
doc_list (Stores documents)
cat_list (Stores Categories)
cat_doc_link_table (stores the doc_id & cat_id from the previous two tables)
Here are how they are formed:
CREATE TABLE `cat_doc_link_table` (
`id` int(11) NOT NULL,
`link_cat_id` int(11) NOT NULL,
`link_doc_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE `cat_list` (
`cat_id` int(11) NOT NULL,
`cat_title` varchar(32) NOT NULL,
`cat_color` varchar(20) NOT NULL,
`cat_icon` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 AUTO_INCREMENT=66 ;
CREATE TABLE `doc_list` (
`doc_id` int(11) NOT NULL,
`doc_title` varchar(50) NOT NULL,
`doc_content` text NOT NULL,
`doc_created` datetime NOT NULL,
`user_id` int(11) NOT NULL,
`doc_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf16 AUTO_INCREMENT=295 ;
put the selected cat_id's in an array(). then use in_array() to check.
// query your db to return an array of cat_id's for the specified post.
$cats_array = array('123', '124', '156');
foreach($category as $cat){
// compare
if(in_array($cat['cat_id'], $cats_array)) {
// cat checked
}else{
// not checked
}
}

How to streamline this (rather large) MySQL query in PHP

I thought I was pretty good at this sort of thing but...
I have the following query from a customer table that is about 8,500 rows and an address table that is about the same number of rows.
This query takes about 10 seconds to complete and I can't figure how to get it to milliseconds.
What am I doing wrong?
<select>
<option value=""></option>
<?php // get the customers
$sql = "
SELECT `cust`.`custid`, `cust`.`custname`, `cust`.`custactive`, `address`.`addtype`, `address`.`address1`, `address`.`addcity`, `address`.`addstate`
FROM `cust`
LEFT Join `address`
ON `cust`.`custid` = `address`.`addcustid`
and `address`.`addtype` = 'b'
WHERE `cust`.`custactive` = 'y'"
;
$result = mysqli_query($con,$sql) or die('Query failed: Could not get list of CLIENTS: ' . mysqli_error($con)); // query
while ($row = mysqli_fetch_array($result)) {
$custid = $row['custid'];
$space="";
$endingspaces = 4-(2*strlen($prodid));
for($count=1; $count<$endingspaces; $count++){
$space .=" ";
}
$custid = $row['custid'];
$custname = substr($row['custname'],0,15);
$address1 = substr($row['address1'],0,15);
$addcity = substr($row['addcity'],0,15);
$addstate = $row['addstate'];
print "<option value=\"$custid\">$custid: $space$custname, $address1, $addcity, $addstate</option>";
}
?>
</select>
DDL:
CREATE TABLE `cust` (
`custid` int(11) DEFAULT NULL,
`custname` varchar(45) DEFAULT NULL,
`custactive` varchar(255) DEFAULT NULL,
`custcreated` varchar(255) DEFAULT NULL,
`custmodified` varchar(255) DEFAULT NULL,
`cust_dataease_custid` int(11) DEFAULT NULL,
`custtype` varchar(5) DEFAULT NULL,
`custrep` int(11) DEFAULT NULL,
`custsource` varchar(5) DEFAULT NULL,
`custdiscount` decimal(65,30) DEFAULT NULL,
`custrepcomm` varchar(255) DEFAULT NULL,
`custsurcharge` varchar(255) DEFAULT NULL,
`custterms` varchar(12) DEFAULT NULL,
`custups` varchar(255) DEFAULT NULL,
`custnotes` varchar(255) DEFAULT NULL,
`custbilldif` varchar(5) DEFAULT NULL,
UNIQUE KEY `custid` (`custid`),
KEY `cust_dataease_custid_idx` (`cust_dataease_custid`),
KEY `custrep_idx` (`custrep`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `address` (
`addid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`addcustid` int(11) unsigned NOT NULL,
`addname` varchar(200) DEFAULT NULL,
`addtype` enum('b','s') NOT NULL DEFAULT 'b',
`address1` varchar(150) NOT NULL,
`address2` varchar(150) DEFAULT NULL,
`addcity` varchar(150) DEFAULT NULL,
`addstate` varchar(150) DEFAULT NULL,
`addstateother` varchar(150) DEFAULT NULL,
`addzip` varchar(150) DEFAULT NULL,
`addcountry` varchar(150) DEFAULT NULL,
`addnote` tinyblob,
PRIMARY KEY (`addid`),
KEY `add_cust_id_idx` (`addcustid`)
) ENGINE=InnoDB AUTO_INCREMENT=13037 DEFAULT CHARSET=utf8
John Green got me to thinking about the way information was being sent to and from the server, which led me to find the problem. Thanks John.
This is how I fixed it: Apparently, when my loop was iterating, it was "printing" each of the 8500 OPTION lines, which action (i didn't know), is a server-client interaction. So for each row, the server and browser had to have a conversation, which slowed everything down.
I fixed it by having the server compile the entire SELECT box on the server side, including the opening SELECT tags and interacting with the browser once through echoing my "add-to" variable: $select_box.
Now it takes like 1.2 seconds. I can live with that.
Thanks everyone for helping me chase this rabbit.
<?php // get the products
$select_box = "<select name=\"customer\" id=\"customer\" onChange=\"getcustinfo();\" data-placeholder=\"Choose a Customer\" class=\"chosen-select\" style=\"width:227px;\" tabindex=\"1\">
<option value=\"\"></option>";
$sql = "
SELECT *
FROM `cust`
LEFT Join `address`
ON `cust`.`custid` = `address`.`addcustid`
and `address`.`addtype` = 'b'
WHERE `cust`.`custactive` = 'y'"
;
$result = mysqli_query($con,$sql) or die('Query failed: Could not get list of CLIENTS: ' . mysqli_error($con)); // query
while ($row = mysqli_fetch_array($result)) {
foreach ($row as $key => $value){ ${$key} = $value; }
$space="";
$custname = substr($row['custname'],0,15);
$address1 = substr($row['address1'],0,15);
$addcity = substr($row['addcity'],0,15);
$select_box .= "<option value=\"$custid\">$custid: $space$custname, $address1, $addcity, $addstate</option>";
}
$select_box .="</select>";
echo $select_box;
?>

Recipe Finder with PHP and MySQL based on Ingredients

I am developing a cooking recipe-website and i want to create a recipe finder based on the used incredients.
My current finder only works with 3 ingredients right.
The Finder should return the right recipe(s) based on the used incredients (should work with 1-n*)
My Tables:
CREATE TABLE IF NOT EXISTS `INGREDIENTS` (
`ingredients_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
CREATE TABLE IF NOT EXISTS `INGREDIENTS_POS` (
`ingredients_pos_id` int(11) NOT NULL AUTO_INCREMENT,
`ingredients_id` int(11) NOT NULL,
`ingredients_unit` varchar(20) NOT NULL,
PRIMARY KEY (`ingredients_pos_id`),
KEY `ingredients_detail_fk` (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
CREATE TABLE IF NOT EXISTS `RECIPES` (
`recipes_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) COLLATE utf8_bin NOT NULL,
`text` varchar(2000) COLLATE utf8_bin NOT NULL,
`count_persons` int(11) NOT NULL,
`duration` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`date` datetime NOT NULL,
`accepted` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`recipes_id`),
KEY `recipes_user_fk` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=88 ;
CREATE TABLE IF NOT EXISTS `RECIPES_POS` (
`recipes_pos_id` int(11) NOT NULL AUTO_INCREMENT,
`recipes_id` int(11) NOT NULL,
`ingredients_id` int(11) NOT NULL,
`ingredients_value` int(11) NOT NULL,
PRIMARY KEY (`recipes_pos_id`),
KEY `recipe_pos_rec_id` (`recipes_id`),
KEY `recipes_pos_ingredient_fk` (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=58 ;
My buggy Solution (doesn't support count from 1-n):
<?php
include 'db_connect.php';
$q = urldecode(mysql_real_escape_string($_GET['q']));
$parameter = explode ('$',$q);
$var = 0;
//print_r($parameter);
foreach($parameter as $ing)
{
//echo $ing;
$sql = "SELECT ingredients_id FROM INGREDIENTS WHERE name='".$ing."'";
$result = mysql_query($sql,$db) or exit('{"Data":null,"Message":null,"Code":500}');
$row = mysql_fetch_array($result);
$arr_id[$var] = $row['ingredients_id'];
$var++;
}
//print_r($arr_id);
$sql = "SELECT r.recipes_id FROM RECIPES r, RECIPES_POS rp WHERE r.recipes_id = rp.recipes_id ";
foreach($arr_id as $id)
{
$sql .= "AND rp.ingredients_id =".$id . " ";
}
//echo $sql;
$result = mysql_query($sql,$db) or exit('{"Data":null,"Message":null,"Code":500}');
mysql_close($db);
$rec;
while($row = mysql_fetch_array($result))
{
//echo "test";
$_GET['id'] = $row['recipes_id'];
$rec= include('get_recipe_byID.php');
}
//print_r(mysql_fetch_array($result));
if (count($arr_id) == 0)
{
echo '{"Data":null,"Message":null,"Code":404}';
die();
}
?>
I need a better solution for that chase.
Maybe SQL itself will help me to find the right recipes
thx
That query helped me a lot:
select r.recipes_id
from RECIPES r
inner join RECIPES_POS rp on r.recipes_id = rp.recipes_id
where rp.ingredients_id in (4, 6)
group by r.recipes_id
having count(distinct rp.ingredients_id) = 2

how can I make loop within a loop in codeigniter?

I have table faq_categories with following fields
CREATE TABLE IF NOT EXISTS `faq_categories` (
`catid` int(11) NOT NULL AUTO_INCREMENT,
`categoryname` varchar(37) NOT NULL,
`parentid` int(11) DEFAULT NULL,
`description` text NOT NULL,
`metatags` text NOT NULL,
`sorder` int(11) NOT NULL,
`visible` tinyint(4) NOT NULL,
`categoryphoto` varchar(255) NOT NULL,
PRIMARY KEY (`catid`),
KEY `parentid_fk` (`parentid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=52 ;
In my php code I have the following code :
$query="SELECT * FROM categories";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$parentid=mysql_result($result,$i,"parentid");
$categoryphoto=mysql_result($result,$i,"categoryphoto");
$sorder=mysql_result($result,$i,"sorder");
$visible=mysql_result($result,$i,"visible");
echo $id;
echo $name;
// THIS IS WHAT I DON'T KNOW HOW TO DO IN CODEIGNITER
// HOW TO GET THE CATEGORYNAME ON BASE ON PARENTID
if ($parentid) {
//echo $parentid;
$query1="SELECT name as parentname FROM categories WHERE id = ".$parentid;
echo mysql_result(mysql_query($query1),0,"parentname");
} else {
echo "Root Category";
}
How can I do this in codeigniter? If possible, I would like to avoid joins.
First: I think you can check Database class
Second: why don't you want use joins? If you hate it maybe you can use FROM table1,table2 :) IMHO lesser queries is better :)
EDIT
Install an sql-gui (e.g. phpMyAdmin, adminer...) and try some query! An example similar what you need:
SELECT *, catjoin.categoryname AS parentname
FROM categories
JOIN categories AS catjoin ON (catjoin.catid=categories.parentid)
You can extend catjoin.categoryname with IFNULL to returns ROOT category.

Categories