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;
}
}
Related
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;
} }
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
I'm trying to do a query based into a value obtained in a previous query. Something like that:
$variableid = 100;
$query_prev = "SELECT query FROM queries_table WHERE id = 1";
$result_prev = pg_query($pg,$query_prev);
$row_prev = pg_fetch_array($result_prev);
$final_query = $row_prev['query'];
$row_prev['query'] value would be "SELECT * FROM other_table WHERE id = $variableid";
$final_query value at this point is: "SELECT * FROM other_table WHERE id = $variableid"
/* but that I want is this value: */ "SELECT * FROM other_table WHERE id = 100"
Use if statement before you do the next select so:
<?php if ($row_prev['query']= 100){
SELECT .......... etc.
}else
SELECT from other
?>
/Hope that's what you wanted
Solved:
$variableid = 100;
$query_prev = "SELECT query FROM queries_table WHERE id = 1";
$result_prev = pg_query($pg,$query_prev);
$row_prev = pg_fetch_array($result_prev);
$query = $row_prev['query'];
eval("\$final_query = \"$query\";");
Here is my query, I am trying to get numbers from another table using a number from another table here is my query ...
$order_id = $template_vars['{order_name}'];
// Query to find the product id for the current order and then set it to a variable
$query="SELECT product_id FROM ps_order_detail WHERE id_order = $order_id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$Product_id = $row['0'];
// get all the custom part numbers and set them to the variables
$customnumbers ="SELECT API, SWAIM, JOHN_CRANE, SNOW_WELL, MIDAS, QUINN, WILSON, WEATHERFORD, HF, BLACK_GOLD, EDI, SO_CAL_PUMPS, WEST_RIVER
FROM ps_product_part_number WHERE Product_ID = $Product_id";
$secondresult = mysql_query($customnumbers);
$secondrow = mysql_fetch_row($secondresult);
$API = $secondrow['0'];
$SWAIM = $secondrow['1'];
$JOHN_CRANE = $secondrow['2'];
$SNOW_WELL = $secondrow['3'];
$MIDAS = $secondrow['4'];
$QUINN = $secondrow['5'];
$WILSON = $secondrow['6'];
$WEATHERFORD = $secondrow['7'];
$HF = $secondrow['8'];
$BLACK_GOLD = $secondrow['9'];
$EDI = $secondrow['10'];
$SO_CAL_PUMPS = $secondrow['11'];
$WEST_RIVER = $secondrow['12'];
How about doing it in one step with a join?
SELECT ppn.*
FROM ps_product_part_number ppn
join ps_order_detail od on od.product_id = ppn.Product_ID
WHERE od.id_order = $order_id
i have the following query:
$sql_select_todo =
"SELECT * FROM
to_do_items
WHERE
id_project = ".$_SESSION['selected_projectId']."'
AND
id IN ('".$alleTaken."')";
$query_select_todo = mysql_query($sql_select_todo) or die(mysql_error());
while($fetch_todo = mysql_fetch_assoc($query_select_todo)) {
echo $fetch_todo['name'];
}
When i echo the query, i get this:
SELECT * FROM to_do_items WHERE id_project = '7401' AND id IN ('10193,12848')
But the output will only give back one name ($fetch_todo['name'])
But there are 2 id's, so i should get 2 names. What do i do wrong here?
Remove single quote in IN query
Change
$sql_select_todo =
"SELECT * FROM
to_do_items
WHERE
id_project = ".$_SESSION['selected_projectId']."'
AND
id IN ('".$alleTaken."')";
To
$sql_select_todo =
"SELECT * FROM
to_do_items
WHERE
id_project = ".$_SESSION['selected_projectId']."'
AND
id IN (".$alleTaken.")";