Here is my code:
<?php
if($_rate->getCode()=="matrixrate_matrixrate") {
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'select * from `extensa_econt_city_office` WHERE `delivery_type`="to_office" GROUP by `city_id`';
$results = $readConnection->fetchAll($query);
?>
<center><b>Населено място:</b></center>
<select name="shipping_city" id="shipping-city-select" title="">
<option value="">Изберете град</option>
<?php
foreach($results as $row){
$CityCode = $row['city_id'];
$query1 = "select * from `extensa_econt_city` WHERE `city_id`='$CityCode' AND `type`!='с.' ORDER by office_id DESC";
$results1 = $readConnection->fetchAll($query1);
foreach($results1 as $row1){
?>
<option value="<?PHP echo $row1['city_id']; ?>"><?PHP echo $row1['type']; ?> <?PHP echo $row1['name']; ?></option>
<?php
}
}
?>
</select>
For some reason the ordering with ORDER by office_id DESC is not working.
There is no change if I change DESC to ASC or even if I remove the ORDER by statement.
Do you have any suggestion why it is not ordering the rows properly ?
You Missing ``(quotes) around office_id
if you still find that query is not working then print whole query using echo statement..
copy that query and paste in php-myadmin and check which sql exception you getting..
hi maybe you can check your sintaxis your code maybe have a little error
for this i suggest that you run your query on phpmyadmin and check if the result is correct , then is correct the result you have a debug on your code php
this a little example for you
<?php
$link=Conect();
$query="SELECT * FROM clients ORDER BY client_name ASC";
$result=mysql_query($query,$link) or die("Error: ".mysql_error());
if(mysql_num_rows($result) > 0)
{
?>
<table border="0">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>ID</td>
<td>NAME</td>
<td>PHONE</td>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr>".
"<td>".$row["id"]."</td>".
"<td>".$row["client_name"]."</td>".
"<td>".$row["phone"]."</td>".
"</tr>";
} /
}
else
{
echo "don't exist clients for list";
}
mysql_close($link);
?>
</table>
good luck and try
Related
Trying to get counts from 2 different tables into a simple HTML table.
This is my code:
$sql = "SELECT COUNT(*) FROM tasks";
$result = $conn->query($sql);
$rowcount=mysqli_num_rows($result);
if ($result->num_rows > 0) { ?>
<table style="width:100%">
<tr>
<th>Total</th>
</tr>
<?php while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?=$rowcount;?></td>
</tr>
<?php } ?>
</table>
<?php } else { echo "0 results"; } ?>
When I run the code, it shows the amount of rows in the table, but it also creates the amount of rows with the number in it (i.e. 281 rows).
Table
281
281
281
281 etc.
My idea was to copy and paste the above to show a second table set of results (if it was correct), but is there a better way of doing this? I've been looking at how I would display SELECT (select COUNT(*) from tasks) , (select count(*) from quotes) into the following format (HTML):
Table
Count
Tasks
281
Quotes
42000
First of all your query does produces only one row for a table, not 281.
The second - I omit usage of prepared SQL statements with placeholders, which should always be used in a real project whenever applyable.
$rows = [];
foreach(['Tasks', 'Quotes'] as $table ){
$result = $conn->query("SELECT '$table' as 'table', count(*) as 'count' FROM $table");
if( $result )
$rows[] = $result->fetch_assoc();
}
if( empty( $rows ) )
print "0 results";
else {?>
<table style="width:100%">
<tr><th>Table</th><th>Count</th></tr>
<?=implode(
"\n",
array_map(function($row){
return "<tr><td>${row['table']}</td><td>${row['count']}</td></tr>";
}, $rows)
)?>
</table>
<?php }
I've been looking at how I would display SELECT (select COUNT() from
tasks) , (select count() from quotes) into the following format
(HTML)
You can just run the queries query, and use the result of the first to create the first row of the table, then the result of the second to create the second row. Since COUNT queries always return exactly 1 row when there's no GROUP BY, it's quite simple to do really:
$sql1 = "SELECT COUNT(*) FROM tasks";
$result1 = $conn->query($sql1);
if ($row = $result1->fetch_array()) $taskCount = $row[0];
else $taskCount = "error";
$sql2 = "SELECT COUNT(*) FROM quotes";
$result2 = $conn->query($sql2);
if ($row = $result2->fetch_array()) $quoteCount = $row[0];
else $quoteCount = "error";
?>
<table style="width:100%">
<tr>
<th>Table</th>
<th>Count</th>
</tr>
<tr>
<td>Tasks</td>
<td><?php echo $taskCount; ?></td>
</tr>
<tr>
<td>Quotes</td>
<td><?php echo $quoteCount; ?></td>
</tr>
</table>
Another way, if you want the HTML structure to be less repetitive / dependent on the tables queries, is to UNION the SELECTs into a single query:
$sql = "SELECT 'Tasks' AS 'table', COUNT(*) as 'count' FROM tasks";
$sql .= " UNION ";
$sql .= "SELECT 'Quotes' AS 'table', COUNT(*) as 'count' FROM quotes";
$result = $conn->query($sql);
?>
<table style="width:100%">
<tr>
<th>Table</th>
<th>Count</th>
</tr>
<?php
while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo $row["table"]; ?></td>
<td><?php echo $row["count"]; ?></td>
</tr>
<?php
}
?>
</table>
I need to run two statements to return the data I need. The below code will return all needed except the count column which can be retrieved from a different table.
How can I run these two statements in the same code to retrieve the count as well?
Here is the code I have:
<?php
$query = "SELECT * from $CLIENTS_DATABASE order by id DESC";
if ($result = mysqli_query($conn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?> </td>
<td><?php echo $row["url"]; ?> </td>
<td><?php echo $row["secret"]; ?> </td>
<td><?php echo $row["count"]; ?> </td>
</tr>
<?php
}
mysqli_free_result($result);
}
?>
The other statement that the count data I need is this:
$query = "SELECT COUNT(*) as count from visits where id_client='$result[id]'"
Thanks for your time and any help you can provide.
Join the two queries.
$query = "SELECT order.*, IFNULL(c.count, 0) AS count
FROM $CLIENTS_DATABASE AS order
LEFT JOIN (SELECT id_client, COUNT(*) AS count
FROM visits
GROUP BY id_client) AS c
ON c.id_client = order.id
ORDER BY order.id DESC";
can you explain me why when i try this query it returns to me only half of rows?
For example, if $records is made by 4 values it only get row 1 and 3 from DB.
What's wrong?
$query=mysql_query("SELECT * FROM ".DB_PREF."books WHERE book_id IN ('".$records."')");
while($fetch=mysql_fetch_assoc($query))
{
global $book_id, $book_title, $book_description, $book_author_id, $book_author_name, $book_author_surname;
$book_id=$fetch['book_id'];
$book_title=$fetch['book_title'];
$book_description=$fetch['book_description'];
$book_author_id=$fetch['book_author_id'];
$query=mysql_query("SELECT * FROM ".DB_PREF."profiles WHERE user_id='".$book_author_id."'");
$fetch=mysql_fetch_assoc($query);
$book_author_name=$fetch['user_name'];
$book_author_surname=$fetch['user_surname'];
getModule('htmlmodule...');
}
Are you overwriting your $fetch variable in the loop? Maybe try:
$fetch2=mysql_fetch_assoc($query);
Or, even better, use a join in your SQL:
$query=mysql_query("SELECT * FROM ".DB_PREF."books LEFT JOIN ".DB_PREF."profiles ON book_author_id = user_id WHERE book_id IN ('".$records."')");
Then just get everything you want out of that single query.
MY DO WHILE
mysql_select_db($database_lolx, $lolx);
$query_Recordset1 = "SELECT * FROM person";
$Recordset1 = mysql_query($query_Recordset1, $lolx) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
<table border="1">
<tr>
<td>id</td>
<td>emal</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['id']; ?></td>
<td><?php echo $row_Recordset1['emal']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
TEST SITE Look here to see code in action
I have two arrays which when echo'd show exactly what i want it to (look at the test site). I'm trying to get them both into a html table but it is only showing the last entry. I have shoved the entire array code into the table and it works fine (although there all in the same row and not separated) but as it is being used in a html email i'm not sure if this will be safe? I'm sorry if this is a really simple fix i'm new to php/mysql so the simple things seem impossible at the moment. I also know i could no doubt combine the two arrays but im on a KISS mantra at the moment and this is the easiest for me to understand. Any help would be appreciated. Thanks.
<?php
//Get Order Codes
foreach ($ids as $id) {
$sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT 1";
$result = mysql_query($sqlcode);
while($row = mysql_fetch_assoc($result)) {
$codes['codes'] = $row['od_code'];
}
echo "".$codes['codes']."<br />";
}
//Get Product Name
foreach ($ids as $id) {
$sqlname = "SELECT pd_name FROM tbl_product WHERE pd_id = $id";
$result = mysql_query($sqlname);
while($row = mysql_fetch_assoc($result)) {
$names['names'] = $row['pd_name'];
}
echo "".$names['names']."<br />";
}
?>
<table width='550' border='1' align='center' cellpadding='5' cellspacing='1'>
<tr>
<td>Description</td>
<td>Code</td>
</tr>
<tr>
<td> <?php echo "". $names['names'].":"."<br>"?> </td>
<td> <?php echo "". $codes['codes']."<br>"?> </td>
</tr>
</table>
A better solution
Change your logic so that you only use one SQL query and make use of a join.
SELECT p.pd_name,
oc.od_code
FROM tbl_product p
LEFT JOIN tbl_order_code oc ON oc.pd_id = p.pd_id
WHERE p.pd_id = $id
So this should be the following using PDO:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("
SELECT p.pd_name,
oc.od_code
FROM tbl_product p
LEFT JOIN tbl_order_code oc ON oc.pd_id = p.pd_id
WHERE p.pd_id = ?");
if ($stmt->execute(array($id))) {
while ($row = $stmt->fetch()) {
// print out table rows here
}
}
Immediate fix (yuck)
This should solve your immediate problem, but there is a much better way of doing this.
<?php
foreach ($ids as $id) {
$sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT 1";
$result = mysql_query($sqlcode);
$codes = array();
while($row = mysql_fetch_assoc($result)) {
$codes[] = $row['od_code'];
}
//Get Product Name
foreach ($ids as $id) {
$sqlname = "SELECT pd_name FROM tbl_product WHERE pd_id = $id";
$result = mysql_query($sqlname);
$names = array();
while($row = mysql_fetch_assoc($result)) {
$names[] = $row['pd_name'];
}
}
?>
<table width='550' border='1' align='center' cellpadding='5' cellspacing='1'>
<tr>
<td>Description</td>
<td>Code</td>
</tr>
<?php foreach($names as $key => $name): ?>
<tr>
<td> <?php echo $name .":"."<br>"?> </td>
<td> <?php echo $codes[$key]."<br>"?> </td>
</tr>
<?php endforeach; ?>
</table>
$names['names'][] = $row['pd_name'];
<td> <?php foreach($names['names'] as $name){ echo "". $name.":"."<br>" } ?> </td>
Or as suggested in comments:
while($row = mysql_fetch_assoc($result)) {
$names['names'] = $row['pd_name'];
$codes['codes'] = $row['pd_code'];
?>
<tr>
<td> <?php echo "". $names['names'].":"."<br>"?> </td>
<td> <?php echo "". $codes['codes']."<br>"?> </td>
</tr>
<?php } ?>
Please try to use PDO instead for interacting with mysql http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
You have to iterate through the array within the table, the same way you do it above the table-tags.
you are using
foreach($ids as $id)
make table to be populated inside foreach
or else use 2d array $name and $codes and count the array size and loop your table structure for count no of times
My page stops loaded every time I turn on the code below... it looks correct and the tables and fields are correct.
<select name="common" style="width: 136px;">
<?php
$group1 = mysql_fetch_array(mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order"));
while($row = $group1){
echo "<option value=\"$group1\">$group1</option>\n";
}
?>
</select>
<?php
$group1 = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order");
while($row = mysql_fetch_array($group1)){
echo "<option value=\"$row[country]\">$row[country]</option>\n";
}
?>
Try this:
<select name="common" style="width: 136px;">
<?php
$recordset = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order") or die("Error found: " . mysql_error());
while($row = mysql_fetch_array($recordset)){
echo "<option value=\"".$row['country']."\">".$row['country']."</option>\n";
}
?>
</select>