When I add $elan_category, I get category_id, instead category_title.
Tried to apply "left join" but no success. I have following tables in database:
function getElanDetail()
{
global $con;
if (isset($_GET['elan_id'])) {
$elan_id = $_GET['elan_id'];
$get_elan = "select * from elan where elan_id='$elan_id'";
$run_elan = mysqli_query($con, $get_elan);
while ($row_elan = mysqli_fetch_array($run_elan)) {
$elan_id = $row_elan['elan_id'];
$elan_category = $row_elan['elan_category'];
$elan_title = $row_elan['elan_title'];
$elan_description = $row_elan['elan_description'];
$elan_image = $row_elan['elan_image'];
$elan_contact = $row_elan['elan_contact'];
echo "
$elan_category //Getting ID of category instead Title :(
$elan_title
$elan_description
$elan_image
$elan_contact
";
}
}
}
With join you can do something like:
$elan_id = $_GET['elan_id'];
$get_elan = "SELECT * FROM `elan`
JOIN `categories` ON `categories`.category_id = `elan`.elan_category
WHERE `elan`.elan_id='$elan_id'";
$run_elan = mysqli_query($con, $get_elan);
while ($row_elan=mysqli_fetch_array($run_elan)){
print_r($row_elan);
// see the keys in $row_elan and use them accordingly
}
For subcategories try this query:
SELECT * FROM `elan`
JOIN `categories` ON `categories`.category_id = `elan`.elan_category
JOIN `subcategories` ON `subcategories`.subcategory_id = `elan`.elan_subcategory
WHERE `elan`.elan_id='$elan_id'
$elan_category = $row_elan['elan_category'];
add these two lines after above code
$cat = mysqli_fetch_row(mysqli_query($con,"SELECT category_title FROM categories WHERE category_id = $elan_category"));
$cat_name = $cat[0];
$cat_name is your category name enjoy
Related
I want to show the selected query from the table
<?php
$where = "";
if(isset($_GET['category']))
{
$catid = $_GET['category'];
$where = " WHERE product.categoryid = $catid";
}
Thus is the $where function to get the category
$sql = "SELECT category.*,
category.category_id , items.id AS itemID,items.asset_tag,items.name,
items.brand,items.status,
items.quantity,items.category_id,
items.color,items.texture,items.photo,items.date,
items.fetch_item FROM items LEFT JOIN category ON
category.category_id=items.category_id
WHERE items.status = 'Available' AND items.fetch_item = '0' $where";
?>
Looks like you did small mistake, because in your "default" query you have already WHERE statement, so in the if you should use AND or OR instead of WHERE again:
$where = "";
if(isset($_GET['category']))
{
$catid=$_GET['category'];
$where = " AND product.categoryid = $catid";
}
additionally you could make your injection of additional WHERE condition a bit more elegant by using sprintf() function. Take a look on example:
$additional_condition = 'AND active=1'
$statement = 'SELECT * FROM users WHERE condition=1 %s';
echo sprintf($statement, $additional_condition);
// Output: SELECT * FROM users WHERE condition=1 AND active=1
I am doing sum with the below query but it is not giving result properly.
If there are four items and it is showing the result like:
1.000 2.000 3.000 4.000 and it should be like 10.000
I don't know where I am mistaken please help.
<?php
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTC = $torder['item_id'];
$qtyT = $torder['qty'];
$chTP = mysql_query("
select * from temp_choices
where item_id = '".$prITTC."'
AND ses_mem = 113
AND status = 1
");
while($chGET = mysql_fetch_array($chTP)){
$fID = $chGET['id'];
$field = $chGET['choice_id'];
$order_tempCHP = mysql_query("
select sum(price) as total, id, ename, choice_id, item_id, price
from choice_price
WHERE
id IN('$field')
");
while ($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
}
?>
according to my question above after trying and trying i found the solution and resolved my problem according to below code:
Thanks to #John Kugelman at MySQL query using an array
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTD = $torder['id'];
$prITTC = $torder['item_id'];
$chTPaa = mysql_query("
select choice_id
FROM temp_choices
WHERE item_id = '$prITTC'
AND ses_mem = 113
AND status = 1
group by choice_id
");
while ($chGETaa = mysql_fetch_assoc($chTPaa)){
$temp[] = $chGETaa['choice_id'];
}
$thelist = implode(",",$temp);
$order_tempCHP = mysql_query("
select sum(price) as total
from choice_price
WHERE
id IN ($thelist)
AND
item_id = '".$prITTC."'
");
while($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
hello I have this variable $company->id
and I have function like this
<?php
function GetEmploees(){
$db = JFactory::getDBO();
$query = 'SELECT * FROM #__jbusinessdirectory_attribute_options AS c JOIN #__jbusinessdirectory_company_attributes AS cp
on c.id = cp.option_id';
$db->setQuery($query);
if( $rows = $db->loadObjectList() ) {
foreach( $rows as $row ){
echo $row->name;
}
}
}
echo GetEmploees($company->id);
?>
I want select $row->name as $company->id
how can I insert code WHERE company_id = '.$company->id.' in query?
company_id is in table #__jbusinessdirectory_company_attributes
Try
$sql = "SELECT * FROM #__jbusinessdirectory_attribute_options AS c INNER JOIN #__jbusinessdirectory_company_attributes AS cp (on c.id = cp.option_id) WHERE c.company_id = '.$company->id.'";
Should do the trick
if user select anything besides "all" it will lead to a where statement involving KodDaerah
$where = '';
$sql= "SELECT * FROM maklumatakaun
LEFT JOIN detailakaun ON maklumatakaun.id = detailakaun.id
LEFT JOIN maklumatbilakaun ON maklumatakaun.NoAkaun = maklumatbilakaun.NoAkaun
LEFT JOIN kodjenisakaun ON detailakaun.KodJenisAkaun = kodjenisakaun.KodJenisAkaun
LEFT JOIN kodlokasi ON detailakaun.KodLokasi = kodlokasi.KodLokasi
LEFT JOIN kodkategori ON maklumatakaun.KodKategori = kodkategori.KodKategori
LEFT JOIN koddaerah ON maklumatakaun.KodDaerah = koddaerah.KodDaerah
WHERE maklumatakaun.KodKategori = '$KodKategori'
AND detailakaun.KodJenisAkaun = '$KodJenisAkaun'
AND maklumatbilakaun.BulanBil = '$BulanBil'
AND maklumatbilakaun.TahunBil ='$TahunBil'
".mysql_real_escape_string($where)."
ORDER BY koddaerah.NamaDaerah ";
if($KodDaerah != "all"){
$where = "AND maklumatakaun.KodDaerah = '$KodDaerah' "; //Add your where statement here
//Whatever you want to do
}
else {
$where = "";
}
if user select all then the system will just use the current sql statement that i provided, for now im not sure what's wrong, so here is where user select the KodDaerah from drop down list box
<?php include('dbase.php');
$sql = "SELECT KodDaerah, NamaDaerah FROM koddaerah";
$result = mysql_query($sql);
echo "<select name='KodDaerah' id='KodDaerah' class='input_field' required />
<option>Pilih Daerah</option>
<option value='all'>Seluruh Pahang</option>";
while ($kod = mysql_fetch_array ($result)){
echo "<option value=".$kod['KodDaerah'].">" .$kod['NamaDaerah']."</option>
<option value='all'>Seluruh Pahang</option>";
}
echo "<?select>";
?>
The problem is now even when i select a certain KodDaerah, it will just run the provided sql query without using the WHERE statement in the $where. Can anybody help?
EDIT:
$sql = $sql . $where;
$result = mysql_query ($sql);
$BilAkaun = mysql_num_rows ($result);
try to move up the if statement:, remove mysql_real_escape_string
(escape before)
$where = '';
if($KodDaerah != "all"){
$where = "AND maklumatakaun.KodDaerah = '$KodDaerah' "; //Add your where statement here
//Whatever you want to do
}
else {
$where = "";
}
$sql= "SELECT * FROM maklumatakaun
LEFT JOIN detailakaun ON maklumatakaun.id = detailakaun.id
LEFT JOIN maklumatbilakaun ON maklumatakaun.NoAkaun = maklumatbilakaun.NoAkaun
LEFT JOIN kodjenisakaun ON detailakaun.KodJenisAkaun = kodjenisakaun.KodJenisAkaun
LEFT JOIN kodlokasi ON detailakaun.KodLokasi = kodlokasi.KodLokasi
LEFT JOIN kodkategori ON maklumatakaun.KodKategori = kodkategori.KodKategori
LEFT JOIN koddaerah ON maklumatakaun.KodDaerah = koddaerah.KodDaerah
WHERE maklumatakaun.KodKategori = '$KodKategori'
AND detailakaun.KodJenisAkaun = '$KodJenisAkaun'
AND maklumatbilakaun.BulanBil = '$BulanBil'
AND maklumatbilakaun.TahunBil ='$TahunBil'
".$where."
ORDER BY koddaerah.NamaDaerah ";
and then do not append $where again
$result = mysql_query ($sql);
$BilAkaun = mysql_num_rows ($result);
I have a table schedules that contains sched_id, sc_id1, sc_id2, sc_id3, sc_id4, sc_id5, sc_id6, sc_id7, sc_id8, sc_id9, sc_id10, sched_name.
I have also table subject_current that has sc_id, sl_id, schoolyear, semister, etc... sc_id1 - scid10 is a "foreign key" from sc_id of table subject_current
Also, I have a table subject_list with sl_id, subject_code, subject_description, subject_prereq. sl_id from table subject_current is a "foreign key" from sl_id of table subject_list.
Now, what I want to do is to "echo" the subject_description from table subject_list only giving me the value of sc_id1 - sc_id10 from table schedules.
This code doesn't work:
for($jaa = 1;$jaa < 11;$jaa++){
$s_scid = "s_scid".$jaa;
$s_sublist = mysql_query("SELECT * FROM subject_current WHERE sc_id='$s_scid'");
while($rows_ss = mysql_fetch_assoc($s_sublist)){
$ss_slid = $rows_ss['sl_id'];
$ssl_sublist = mysql_query("SELECT * FROM subject_list WHERE sl_id='$ss_slid'");
while($rows_ssl = mysql_fetch_assoc($ssl_sublist)){
$ssl_slid = $rows_ssl['sl_id'];
$ssl_subdesc = $rows_ssl['subject_description'];
}
}
echo $ssl_subdesc;
}
EDIT
This is what I want to exactly happen:
$s_scid1 = $rows_s['sc_id1']; // which is a value of 1
$s_scid2 = $rows_s['sc_id2']; // which is a value of 2
$s_sublist = mysql_query("SELECT * FROM subject_current WHERE sc_id='$s_scid1'");
while($rows_ss = mysql_fetch_assoc($s_sublist)){
$ss_slid1 = $rows_ss['sl_id'];
$ssl_sublist = mysql_query("SELECT * FROM subject_list WHERE sl_id='$ss_slid1'");
while($rows_ssl = mysql_fetch_assoc($ssl_sublist)){
$ssl_slid1 = $rows_ssl['sl_id'];
$ssl_subdesc1 = $rows_ssl['subject_description'];
echo $ssl_subdesc1;
}
}
$s_sublist2 = mysql_query("SELECT * FROM subject_current WHERE sc_id='$s_scid2'");
while($rows_ss2 = mysql_fetch_assoc($s_sublist2)){
$ss_slid2 = $rows_ss2['sl_id'];
$ssl_sublist2 = mysql_query("SELECT * FROM subject_list WHERE sl_id='$ss_slid2'");
while($rows_ssl2 = mysql_fetch_assoc($ssl_sublist2)){
$ssl_slid2 = $rows_ssl2['sl_id'];
$ssl_subdesc2 = $rows_ssl2['subject_description'];
echo $ssl_subdesc2;
}
}
This is a pain to write it 10 times. So I want to loop it. But someone told me it's bad and told me about INNER JOIN. But how can I with INNER JOIN?
The way you describe it, you should do something like:
for($jaa = 1;$jaa < 11;$jaa++){
$s_scid = "s_scid".$jaa;
$the_query = "SELECT sl_id, subject_description FROM subject_list sl JOIN subject_current sc ON sl.sl_id=sc.$s_scid";
/* the above should generate a JOIN with a particular element from your master table */
$ssl_sublist = mysql_query($the_query);
while($rows_ssl = mysql_fetch_assoc($ssl_sublist)){
$ssl_slid = $rows_ssl['sl_id'];
$ssl_subdesc = $rows_ssl['subject_description'];
echo $ssl_subdesc;
}
}