I'm new to PHP...
We have a table that get lots of rows with data and we need to show only the last 4 rows of it.
This is the code before the table:
while ($counter < $i) { // strat while loop, echo guests
if ($counter == 4) { break; }
right now it displays the first 4 rows
Note that we don't want to use SQL LIMIT 4 because the data can be changed from the user actions that we asks before the while.
Try to use SQL ORDER BY id DESC - this will select your collection in reverce order. (or it could be other column instead id which you are using as primary key)
Related
I'm trying to do a next and previous button based on records from database but i'm not really sure how to do it and which would be the best way.
The way that i was thinking to do it
would be to have a field in the table named position.
and the query would look like this:
SELECT * FROM t1 WHERE step = 1
then in the php i would have something like this
$step = $step + 1 // for the next button
if($step === 1) {
$step = 1;
} else {
$step = $step - 1;
}
But if i would delete a record then my position would be out of sync with the math and i won't be able to do + or - on step field because i would find the record.
Which means everytime i would delete something i would need to do a new increment on that field to keep it synced which might later become a problem.
How should i handle it ?
You can add a row number to a query like this:
select #qposn:=#qposn+1 posn, a.* from sometable a, (select #qposn:=0) x
You can select a specific row, in this case the 2nd row, like this:
select * from (
select #qposn:=#qposn+1 posn, a.* from sometable a, (select #qposn:=0) x
) x where posn=2
But be aware that if the data in the table changes the numbering will change also. If someone deletes the first record in the table then the data that used to be row 2 will now be row 1 and so paging may skip or repeat rows.
If your table is relatively small you might consider caching the table in a javascript array and then paging through the array.
I am just a student at php, I am trying to find out a list of members from a table where I dont know how much rows are in that table, but I need every 6 rows on Order By entrytime DESC basis.
My table structure is as below:
int `ID`
int `entrytime` // this time updates when rows insert
now I need to find out all ids on basis of entrytime DESC and insert those ids in a separate new table say "new_tbl" as a group of 6 ids
ind `ID`
varchar `group_name`
I am trying to do like this :
$qry=mysql_query("SELECT id FROM main_table entrytime ASC");
while($res=mysql_fetch_row($qry)){
$id=$res['0'];
$q=mysql_query();
######### But not getting any Idea how to find every 6 Ids and insert in new_tbl #######
}
In your case you use mysql_fetch_row this gives you the count of rows and not the data.
while($res=mysql_fetch_assoc($qry)) {
To get the Data you have to use mysql_fetch_assoc for example.
To find every 6 Ids you can make a counter and increment the value by 1. If the modulo of 6 and the value is 0 you have the sixth value and you can reset the counter.
$i = 1;
while ...
if($i % 6 == 0) {
// reset your counter
$i = 1;
}
$i++;
}
Then you can use the counter to work with it and write that to another table for example.
I have the following table:
id | name | position
1 Bob 4
2 Jim 5
3 Harry 73
4 Paul 89
I want the user to be able to re-order the columns as they see fit, like move one row up and down, using the position column. I have thrown in the big numbers in there (73 and 89) just to cater for all events.
Is there a way to dynamically re-order the table via SQL? Or will I have to manually re-order all tables when someone selects 'move up' on 'Harry'? I can only imagine I will have to:
Find the ID before Harry's (in this case '2')
Move all ID's (including '2's) up by one.
Set Harry's position to to '2'.
There must be a quicker easier way to do this using MySQL/PHP?
If can you provide position order after pressing each up or down you can use following query. That is if current position is 4,5,73,89, suppose user selected 89 and pressed up. In that case if you can provide 4,5,89,73 to query then following query will help you
select * from table22 order by field(position,4,5,89,73);
you just need to use a simple case in your update statement, see example below for moving a number up. For moving a numberdown, same case with minor adjustment, and you can use a nested case to decide which to use by comparing number and destination
set #nr=4; set #dest=2;
select *,
case
when number between #dest and #nr-1 then number+1
when number=#nr then #dest
else number
end as number_alt
from
(
select 1 as number union select 2 union select 3 union select 4 union select 5
)a
For those that find this page in the future, I found some code from an old shop that did the trick:
if((isset($_POST['moveup_x'])) || (isset($_POST['movedown_x']))) {
$current = select("SELECT `position` FROM `events` WHERE `id`='".rEsc($eventid)."';", true,true);
if(isset($_POST['moveup_x'])) $target = ($current - 1);
if(isset($_POST['movedown_x'])) $target = ($current + 1);
if($current > 1) {
$counter = 1;
foreach(select("SELECT `id` FROM `events` ORDER BY `position`;") as $val)
{
$newposition = $counter;
if($current == $newposition) {
$newposition = $target;
} elseif(($current > $newposition) && ($target <= $newposition)) {
$newposition++;
} elseif(($current < $newposition) && ($target >= $newposition)) {
$newposition--;
}
runSQL("UPDATE `events` SET `position`='{$newposition}' WHERE `id`='{$val['id']}';");
$counter++;
}
array_push($msgs,"Successfully moved event.");
} else {
array_push($msgs,"!Cannot move the event any higher up the list.");
}
}
You need to know your current, which you can get from $_POST (in this case I got from rowID in the database) and your target (I had img submit buttons so I just minused or plussed from whichever button the user clicks). Once you have that, loop through each row in the table however you see fit, (set a counter beforehand) and run the IF/ELSE statement. Modify for your need.
It works very well though. There problably is a quicker/easier way, i.e. all in one SQL statement, instead of updating every single row in the database, but I couldnt find it.
I have created a question Here That I think is a little bloated. To skim down this question here is my requirements:
I need to create 3 mySQL queries that grab all information in that row so that I can format the information into one of HTML tables.
Here is the query that I have which selects everything:
$qry_questions = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_votes DESC);
while($row = mysql_fetch_array($qry_questions){
//Grab all information
}
Now, I need to make three queries that act like the one above, but I need it to do something like this:
pull everything from every third row in a table starting at row #1
pull everything from every third row in a table starting at row #2
pull everything from every third row in a table starting at row #3
Then I would put each of those queries into one of the 3 columns.
I can not do this by unique ID. Yes, it is an auto incrementing ID, but it will be probable that I might have to move whole rows into another table.
EDIT: I've added my attempt to count each row and place the queried result into the right "bin"
//GIVE EACH ENTRY A COLUMN NUMBER
$count++;
if($count >= 4){
$count = 1;
}?>
<div class="col" id="col-1">
<?PHP
while($count == 1){
include("pieces/answers/newAnswerLayout.php");
}
?>
</div>
<div class="col" id="col-2">
<?PHP
while($count == 2){
include("pieces/answers/newAnswerLayout.php");
}
?>
</div>
<div class="col" id="col-3">
<?PHP
while($count == 3){
include("pieces/answers/newAnswerLayout.php");
}
?>
</div>
Here's one approach, to get the resultset returned by MySQL. (But it might be easier to just return all the rows, and get every third row within the app). But it can be done in MySQL pretty easily. Note that your original query is wrapped in parens (as an inline view) aliased as r.
SELECT r.*
FROM (
SELECT *
FROM mbr_qa_questions
ORDER BY q_votes DESC
) r
CROSS
JOIN ( SELECT #i := 0 ) s
HAVING ( #i := #i + 1) MOD 3 = 1
That will return every third row, starting with the first row. To get every third row starting with the 2nd and 3rd row, replace the literal = 1 in the HAVING clause with = 2 or = 3 (respectively).
Why do you need three different queries? Can you not, instead, keep the single query you have, have a counter that is incremented in every iteration of the loop, and on %3 does whatever you would do for cases #1, #2 or #3?
you can probably just use a counter to do it in a much simpler fashion, but I would do it like this:
$questions = array();
$results = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_votes DESC");
while($row = mysql_fetch_array($results)){
$questions[] = $row;
}
for($i = 0; $i < count($questions); $i++) {
if($i + 1 % 3 == 0) {
$questions[$i];//this is a 3rd, so do something with it
}
}
How can I make a limit of showing the results? I need to limit it for 100 views.
In DB I have:
ID|NAME|PAGE|COUNT|DATE
In count I want to count untill 100 and then stop showing that ID. I could do it with count < 100. And then update the specific ID. I could get records with less than 100 views, but I couldn't manage to update count on the specific ID.
Row is showed with:
php code:
foreach($bannerGroups[0] as $ban) {
echo '<li class="right1">'.$ban->html().'</li>';
}
But I just don't know where to put the update in there. I tried, but all I got was to update only one ID. But it shows 4 on one page and randomizes them on refresh. So I don't know what to do.
Also I would like to say I am only learning php. Sorry for all the mess.
Code at http://pastebin.com/A9hJTPLE
If I understand correctly, you want to show all banners that have been previously-displayed less than 100 times?
If that's right, you can just add that to your WHERE clause:
$bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' WHERE `COUNT` < 100");
To update them all, you can either run a query while displaying each individual banner, or "record" the id of each and run a single query at the end, like:
$ids = array();
foreach($bannerGroups[0] as $ban) {
$ids[] = $ban['ID']; // record the ID; don't know how Banner
// class works, assuming uses indexes; maybe ID() method?
echo '<li class="right1">'.$ban->html().'</li>';
}
...
mysql_query('UPDATE table SET `COUNT` = `COUNT` + 1 WHERE ID IN (' . join(',', $ids) . ')');
UPDATE:
Based off of a comment, your Banner class doesn't have a method to retrieve the individual banner's ID. In this case, you can record the ID values when you're building your banners array:
$ids = array();
while($row=mysql_fetch_assoc($bannerResult)) {
$banners[] = new Banner($row);
$ids[] = $row['ID']; // record the ID
}
// update the `count` on each record:
mysql_query('UPDATE table SET `COUNT` = `COUNT` + 1 WHERE ID IN (' . join(',', $ids) . ')');
sorry, but I got your question wrong...
first you have to insert a new sql-column like "viewcount" to the db...
on every read, you have to increment the value in viewcount...
for that behaviour (because, mysql does not allow sub-selects on update-clause on the same table), you have to fetch the results from db, as you do that, and pass all the primary-keys of the records to an array...
after the view-logic you have to fire up a query like:
UPDATE foo SET viewcount = viewcount + 1 WHERE id IN (1,2,3,4,5,6...,100);
where the IN-clause can be easily generated using your primary-keys-array with "implode(',', $arr);"
hope this helps.
$bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' AND `count`<100");
#newfurniturey figured it out. in each foreach($banneruGroups added: $ids = $ban->getValue('id'); and then mysql_query("UPDATE dataa SET COUNT = COUNT + 1 WHERE id = '$ids'"); but is there any way to update them by adding query only once? And if the id is showed already 100 times i get Warning: Invalid argument supplied for foreach() in. Any idea how to fix it? I have 4 ids in DB . If one of them already have 100 views (count) then i get error!
Try to limit your data source for 100 items.
It's like OFFSET x LIMIT 100 in MySQL/PostgreSQL query or TOP 100 in MSSQL.