Each page lists all the coupons available for a specific retailer. I query the database for all the coupon codes in the header since I count the number of rows returned and use that info in the meta title of the page. I now also want to display the titles of the first 2 coupons in the array. How would I go about extracting the first 2 results from the array without querying the database again?
This is what I have so far:
$retailer_coupons = "select C.couponid,C.fmtc_couponid,C.merchantid,C.exclusive,C.label,C.shoppingtip,C.restrictions,C.coupon,C.custom_order,C.link,C.image,C.expire,C.unknown,M.name,M.approved,M.homepageurl,M.category from tblCoupons C,tblMerchants M where C.merchantid=M.merchantid and C.begin < ".mktime()." and C.expire > ".mktime()." and C.merchantid=".$merchantid." and M.display='1' and C.user_submitted='' order by C.custom_order desc, C.coupon desc";
$retailer_coupons_result = mysql_query($retailer_coupons) or die(mysql_error());
$count_coupons=mysql_num_rows($retailer_coupons_result);
$meta_title = ''.$name.' Coupon Codes ('.$count_coupons.' coupons available)';
Suppose I have 3 records in my table. If I execute below query, I will get 2 results however the count(*) will give me 3 as output
SELECT count(*) FROM temp.maxID limit 2
In your case it will be
$retailer_coupons =
"select C.couponid,C.fmtc_couponid,C.merchantid,C.exclusive,C.label,C.shoppingtip,C.restrictions,C.coupon,C.custom_order,C.link,C.image,C.expire,C.unknown,M.name,M.approved,M.homepageurl,M.category
from tblCoupons C,tblMerchants M
where C.merchantid=M.merchantid
and C.begin < ".mktime()." and C.expire > ".mktime()."
and C.merchantid=".$merchantid." and M.display='1'
and C.user_submitted=''
order by C.custom_order desc, C.coupon desc
limit 2";
limit 2 will do the magic... Cheers!!!
Good Luck!!!
Something like this:
$res = mysql_fetch_assoc($retailer_coupons_result);
$i = 0;
while ($i < 2){
echo $res[$i]['label']."\n";
$i++;
}
Related
I'm trying to give my users a rank based on the amount of posts they posted. I made a database containing a rankName row with "beginner, novice, itermediate,... to master" and a minimum row with some numbers. I tried to compare the amount of posts ($qtyPosts) with the minimum rows.
For example: When a user has 9 posts, he gets the rank Novice (which has a minimum of 5 posts).
This is the code i wrote for that.
PHP code
// calculate number of posts from user
$rowsPosts = $user->getQuantityOfPosts($userID);
$qtyPosts = 0;
foreach ($rowsPosts as $q) {
$qtyPosts++;
}
//status
$conn = db::getInstance();
$rank = "";
$statementRank = $conn->prepare("SELECT * FROM rank WHERE rank.minimum >= $qtyPosts");
$statementRank->execute();
while($row = $statementRank->fetch(PDO::FETCH_ASSOC) ){
$rank = $row['rankName'];
}
HTML code
<h3>Status: <?php echo $rank; ?></h3>
However, It doesn't post the right rank, instead it posts the latest one, "master". Anyone any idea?
Consider your WHERE clause:
WHERE rank.minimum >= $qtyPosts
If the user is at the lowest rank, then all ranks will be >= that user's post count.
You can keep the same logic, but simply add an order and limit. Something like this:
WHERE rank.minimum >= $qtyPosts ORDER BY rank.minimum LIMIT 1
This would sort the ranks from lowest to highest and just select the first one.
I guess you should have a maximum column as well in the table and change the query to
SELECT * FROM rank WHERE rank.minimum >= $qtyPosts AND rank.maximum < $qtyPosts
Since according to your current post, you'll get all the ranks with $qtyPosts > the minimun number.
I have a small query that sums the totals of columns, is there a way I can ORDER BY ASC so that the result displays the rows with the biggest total first.
My query is:
select
sum(SeqID0101 = 1) as SeqID0101,
sum(SeqID0102 = 1) as SeqID0102,
sum(SeqID0103 = 1) as SeqID0103,
sum(SeqID0104 = 1) as SeqID0104,
sum(SeqID0105 = 1) as SeqID0105,
sum(SeqID0106 = 1) as SeqID0106,
sum(SeqID0107 = 1) as SeqID0107,
sum(SeqID0108 = 1) as SeqID0108,
sum(SeqID0109 = 1) as SeqID0109,
sum(SeqID0110 = 1) as SeqID0110
from
PH001_Hist
EDIT
Hi and thanks for your replies.
I am using the code below to produce tow columns, 1 with the name of the field, 2 with the data content. This works as I require. But when the query sums the content of the fields I need to order the result to display the largest field content first.
Example:
Fieldname FieldData
SEQID0101 - 8
SeqID0108 - 6
SeqID0103 - 2
and so no.
$row = mysql_fetch_assoc($FailedList);
echo '<table>';
foreach ($row as $k => $v)
echo '<tr><td>'.$k.'</td><td >'.$v.'</td></tr>';
echo '</table>';
I just can't get the code right to produce the require result.
Any help would be great.
Try that:
ORDER BY GREATEST(SeqID0101,SeqID0102,SeqID0103,SeqID0104 , .....) DESC
I want to get all rows count in my sql.
Table's first 2 columns look like that
My function looks like that
$limit=2;
$sql = "SELECT id,COUNT(*),dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
$stmt = $this->db->prepare($sql);
$stmt->execute();
$stmt->bind_result($id, $total, $datetime, $title, $content);
$stmt->store_result();
$count = $stmt->num_rows;
if ($count > 0) {
while ($stmt->fetch()) {
Inside loop, I'm getting exact value of $total, but MySQL selects only 1 row - row with id number 1. (and $count is 1 too)
Tried this sql
SELECT id,dt,title,content FROM news ORDER BY dt DESC LIMIT 2
All goes well.
Why in first case it selects only 1 row? How can I fix this issue?
for ex my table has 5 rows. I want to get 2 of them with all fields, and get all rows count (5 in this case) by one query.
Remove COUNT(*). You will only ever get 1 row if you leave it in there.
Try adding GROUP BY dt if you want to use COUNT(*) (not sure why you're using it though).
EDIT
Fine, if you insist on doing it in a single call, here:
$sql = "SELECT id,(SELECT COUNT(id) FROM news) as total,dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
This is likely cause by the variable $limit being set to 1, or not being set and mysql defaulting to 1. Try changing your first line to
$sql = "SELECT id,COUNT(*),dt,title,content FROM news ORDER BY dt DESC";
EDIT
Change to:
$sql = "SELECT SQL_CALC_FOUND_ROWS,id,dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
And then use a second query with
SELECT FOUND_ROWS( )
to get the number of rows that match the query
This totally wreaks of a HW problem... why else besides a professor's retarded method to add complexity to a simple problem would you not want to run two queries?
anyways.... here:
SELECT id, (SELECT COUNT(*) FROM news) AS row_count, dt, title, content FROM news ORDER BY dt DESC LIMIT
Faily new to php and mysql, this will probably seem very messy.
This is what I came up with:
$query = "show tables like 'whatever%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for ($i = 0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
$sql=mysql_query("SELECT * FROM ". $row[0] ." WHERE a=(SELECT MAX(a)) AND b=(SELECT MAX(b)) AND c LIKE 'd%' ORDER BY date DESC LIMIT 1");
while($info=mysql_fetch_array($sql)){
echo "...";
}
}
I get the desired value from each table, so x results depending on the amount of tables. What I would like is to have the results of the queried tables but only show the top 10-5 ordered by date/time.
Is this possible with the current script? Is there an easier way (while, number of tables changing constantly)? Is this query method database intensif?
Cheers!
I call constantly changing number of tables having similar structure a design error. also query switch to
$sql=mysql_query("SELECT * FROM $tbl WHERE c LIKE 'd%' ORDER BY a DESC, b DESC, date DESC LIMIT 1");
is a little relief to database.
I have a question probably lame but it made me stuck
I have the a db query
$query_Recordset10 = "SELECT * FROM products
WHERE razdel='mix' AND ID='$ID+1' AND litraj='$litri' ORDER BY ID ASC";
$Recordset10 = mysql_query($query_Recordset10, $victor) or die(mysql_error());
$row_Recordset10 = mysql_fetch_array($Recordset10);
$totalRows_Recordset10 = mysql_num_rows($Recordset10);
This is the query for the next product in the line based in the ID of the current product thats on the page.
But if the next product matching the criteria in the query is 2 or more ID's ahead my cycle breaks. So is there a way for skipping this rows and get the next ID matching the criteria.
Thank you very much.
SELECT * FROM products
WHERE razdel='mix' AND ID > $ID AND litraj='$litri' ORDER BY ID ASC";
PROBLEM SOLVED. I still have a lot to learn.
SELECT * FROM products WHERE razdel='mix' AND ID>'$ID' AND litraj='$litri' ORDER BY ID ASC LIMIT 1
this is the wright line but my mistake was how $ID is generated.
Thank you all.
Change your query to this:
$query_Recordset10 = "SELECT * FROM products
WHERE razdel='mix' AND ID > '$ID' AND litraj='$litri' ORDER BY ID ASC LIMIT 1";
So you still only get 1 row returned (if there's anything to return), but you'll be returning the next row (according to ORDER BY ID ASC) versus (potentially) the row with an incremental ID.
Use foreach to loop over an array rather than trying to access by indexes (if you're fetching every value):
foreach ($records as $record) {
}
instead of
for ($i = 0; $i < count($records); $i++) {
}
or
$i = 0;
while ($i < count($records)) {
$i++;
}
or
$i = 0;
do {
} while (++$i < count($records));
Use this query instead:
SELECT * FROM products WHERE razdel='mix' AND ID>$ID AND litraj='$litri' ORDER BY ID ASC LIMIT 1
This will give you the next element after the one with ID of $ID.