Jump to content

[SOLVED] query help


bhavin_85

Recommended Posts

hi guys

 

im trying to replicated 1 query in multiple text boxs on a form, the first box is comin up with the results but the rest are jsut blank

 

any ideas?

 

<?
session_start();
if ( empty($_SESSION['username'])){
header("location:default.php");
exit;
}
include('../../config.php');

$sql="SELECT * FROM item";
$query=mysql_query($sql) or die(mysql_error());

.....

<select type="text" id="item_item_id" name="item_item_id">
<? while ($row = mysql_fetch_array($query))
{?>
<option value="<? printf($row["item_id"]); ?>"><? printf($row["item_name"]);?></option>
<? } ?>
</select>
</td>
<td colspan="1">
<input id="description" name="description" type="text"></input></td>
<td colspan="1">
<input name="weight" id="weight" type="text"></input></td>
<td colspan="1">
<input name="price" id="price" type="text"></input></td>
</tr>
<tr>
<td colspan="1">
<select type="text" id="item_item_id" name="item_item_id">
<? while ($row1 = mysql_fetch_array($query1))
{?>
<option value="<? printf($row1["item_id"]); ?>"><? printf($row1["item_name"]);?></option>
<? } ?>
</select>

 

ive tried 2 change the variable names but that still didnt work  ??? any ideas?

Link to comment
https://forums.phpfreaks.com/topic/42141-solved-query-help/
Share on other sites

great that works :)

 

i was wondering if u could help me with another issue im having

ive created a page which pulls item ids from a table and i need it to calculate which item id comes up the most then show an image depending on that id

 

so say in the database i had store 1,1,2,3,4,4,4 it would count that there are 2 * 1, 1* 2, 1 *3, 3*4 in the table and then print an image associated with 4

 

the image part i can do its sut the count function doesnt work  ???

 

<?
session_start();
if ( empty($_SESSION['username'])){
header("location:default.php");
exit;
}

$id=$_SESSION['cust_id'];

include ('config.php');

$sql = "SELECT invoice_id FROM invoices WHERE cust_id='$id'";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {
    $invoice_id = $row['invoice_id'];
//	echo "Invoice ID: ".$row['invoice_id'];

$sql1 = "SELECT item_item_id FROM invoice_items WHERE invoices_invoice_id=$invoice_id ORDER BY item_item_id  DESC";

//echo $sql1;
$query1 = mysql_query($sql1) or die(mysql_error());
while ($row1 = mysql_fetch_assoc($query1)) { 
    echo "Item ID: ".$row1['item_item_id'];
}
$row2 = mysql_fetch_assoc($query1);
$num_arrays = count($row2);
echo "Total items: $num_arrays";  
}

?>
<html>
<head>
<title>P.B.L. Jewellers Ltd</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#030102" text="#666666">
<div>
<table width="670" height="546" border="0" align="center" bordercolor="#000000">
  <tr>
      <td colspan="2" align="right" height="10">
        <? 
  echo "You are logged in as " . $_SESSION['uname'] . ""?>
        <a href="default.php">Logout</a> </td>
  </tr>
  <tr> 
    <td colspan="3" valign="top" width="670" height="158"> <img src="images/banner2.png" width="670" height="158" align="top"> 
    </td>
  </tr>
  <tr>
  <td colspan="3" align="center" height="20"><? readfile("menu_promo.inc"); ?>
  </td>
  </tr>
<tr>
     <td></td>
</tr>
    <tr> 
      <td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/42141-solved-query-help/#findComment-204736
Share on other sites

<?php
$sql = "SELECT invoice_id , COUNT(*) as total
          FROM invoices 
          WHERE cust_id='$id'
          GROUP BY invoice_id
          ORDER BY total DESC
          LIMIT 1 ";
$res = mysql_query ($sql);
$most_frequent_id = mysql_result ($res, 0, 0);
?>

Link to comment
https://forums.phpfreaks.com/topic/42141-solved-query-help/#findComment-204738
Share on other sites

Sorry, should be invoice item id and not the invoice id  :-[

 

Change the query to

 

SELECT i.item_item_id, COUNT(*) as total
FROM invoices v INNER JOIN invoice_items i ON v.invoice_id = i.invoices_invoice_id
WHERE v.cust_id='$id'
GROUP BY i.item_item_id
ORDER BY total DESC
LIMIT 1

Link to comment
https://forums.phpfreaks.com/topic/42141-solved-query-help/#findComment-204741
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.