PHP - Last Db Items?
How can I select the last 5 db items? I was thinking a timestamp or some thing.
Similar TutorialsI have a webpage where all the database items are displyes in a table format.The table also has a check box.Upon clicking the delete button i need to delete all the items whish has the checkbox checked. How will i do that What is the best way to capture items that someone wants to purchase when they don't have an account yet? And I prefer not using cookies.
Guys, i want to display ads after 4 items to be displayed. Iam fetching item using mysql database & iam showing 8 items per page. I used below code but its not working because i used the item id to specify the first number but the items is not listed as correct order Code: [Select] <?php // first number $first = $row['id']; // second number $second = 4; //checking if the first number is muliple of second. If true then display ads. If($first % $second == "0") { echo 'ads to display'; } It will work if i list items in correct order. Please let me know correct solution. Thanks in advance ! am currently building the cart system of a product, now there is this part wherein, the non-logged-in OR logged-in user should also be able to see the items that he/she viewed, how to do that?., am not asking for code snippets , just give me some ideas/hints/strategies/tips that may help me get the big picture on how to do this thing and proceed coding. Hi guys! I'm trying to make a <select> button for car positions which displays a set of numbers like 1-5 or 1-10 so on.. I need to remove the numbers that is already been selected. Selected numbers are stored in the database and i fetch them in a simple query then compared it to the max number of cars. I have this code, it kinda works but it shows the number 1 which is already been selected. Selected numbers are 1 and 3, it successfully removes number 3 but not 1, The output is 1,2,4,5,6,7,8,9,10 I added some comments for clarity.. Code: [Select] function position_available($event_id, $number_of_cars) // receives id for ref. in the database { $selected_positions = check_selected_positions($event_id); // query to get selected positions while($positions = mysql_fetch_array($selected_positions)) { $position = array(); array_push($position, $positions['car_position']); // 'car position' is the name of the field } for($i = 1; $i <= $number_of_cars; $i++) // $number_of_cars is the total number of cars { if(! in_array($i, $position)) echo "<option value='{$i}'>" . $i . "</option>"; } } Did i miss something simple? thanks for the help in advance! Is it possible to add up a bunch of selected rows? For example, something like this: $sql = "SELECT (and add up) weight FROM store WHERE id LIKE $id" (there are multiple $id's) Obviously that isn't the answer - but hopefully it gives an idea of what I'm after. Thanks Jack Hello, In my Mysql database, it has a datetime field. and I have created a $today = date('Y-m-d H:i:s'); <- today's date and time How do I write a query to run in PHP in order to get all today's items by comparing datetime field and $today? Thanks! This is probably some obvious error I have made, but I cannot figure it out. I have made a few pages and now I am debugging them. My first page is called insert_purchase_order.php; on this page a person will enter some data in fields and hit the insert button. Then, the data is passed to another page, but when I try to insert into mysql it does not give me any errors, but I have no new rows either. The code for my 2nd page: Code: [Select] <?php session_start(); $action=$_GET[action]; if ($action==insert){ $randid=$_POST['randid']; $vendor=$_POST["vendor"]; $purchase_order_date=$_POST["purchase_order_date"]; $ship=$_POST["ship"]; $fob=$_POST["fob"]; $terms=$_POST["terms"]; $buyer=$_POST["buyer"]; $freight=$_POST["freight"]; $req_date=$_POST["req_date"]; $confirming_to=$_POST["confirming_to"]; $remarks=$_POST["remarks"]; $tax=$_POST["tax"]; $con = mysql_connect("localhost","root","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("main", $con); mysql_query("INSERT INTO purchase_order (randid, vendor, purchase_order, ship, fob, terms, buyer, freight, req_date, confirming_to, remarks, tax) VALUES ($randid, $vendor,$purchase_order_date,$ship, $fob, $terms, $buyer, $freight, $req_date, $confirming_to, $remarks, $tax)"); mysql_close($con); echo 'Data Accepted...'; echo '<br/>'; echo 'P.O. Inserted Successfully'; }else{ echo 'Error... Please Contact Bruce.'; echo 'Bruce, no data was passed from the insert_purchase_order.php page.'; } ?> <a href="http://localhost/insert_purchase_order_items.php?po= <?php echo $randid; ?>">Insert Purchase Order Items</a> I have permissions and everything. Thanks Hi Guys, I have a script that shows the user of my shop what they have looked at. I was wondering if someone can tell me how I can limit this so it only lists 10 and no more. Here is the code: Code: [Select] if(RECENTVIEWED ==1) { if(isset($_COOKIE['jimbeam'])){ $productUrl = SITE_URL."ecom/index.php?action=ecom.pdetails&mode="; $productUrl = $this->libFunc->m_safeUrl($productUrl); foreach ($_COOKIE['jimbeam'] as $name => $value) { $this->obDb->query="SELECT vTitle FROM ".PRODUCTS." WHERE vSeoTitle = '".$value."'"; $rsProd = $this->obDb->fetchQuery(); $this->obTpl->set_var("TPL_VAR_PRODUCTTITLE",stripslashes($rsProd[0]->vTitle)); $this->obTpl->set_var("TPL_VAR_PRODUCTURL",$productUrl.$value); $this->obTpl->parse("recent_blk","TPL_RECENT_BLK",true); } $this->obTpl->parse("mainrecent_blk","TPL_MAINRECENT_BLK"); } } Thanks, Jordan Hi, i have had some help from these forums building a shopping cart system and i can add items to cart, empty cart but i need some help on how to update quantities. Here is the code i use to add items Code: [Select] $pid = $_POST['prodid']; $q = $_POST['qty']; if(array_key_exists($pid, $_SESSION['cart'])) { $_SESSION['cart'][$pid]=$_SESSION['cart'][$pid]+$q; } ELSE { $_SESSION['cart'][$pid]=$q; } Im displaying my items in a table with a text field for quantities which can be changed then click update. i see there is 2 things i need to do, first is to somehow create a loop for all the items in my table, i have no clue where to start on that, and then in that loop i have my update command. im assuming the code to update would be something like Code: [Select] $_SESSION['cart'][$pid]=$_SESSION['cart'][$pid][$qty] where $qty is the value of the text box in my table. Any help would be appreciated.
I'm trying to parse multiple rss feeds and only show and save items from the last month, but i can't get it to work. I've tried this: $pubdate = date("Y-m-d", strtotime($item->pubDate)); if (date($pubdate, strtotime(" -1 month")))
Hi, I'm having trouble carrying items between two files. The problem is that it works too well. Whenever I include a php file that echos something, the other php files echos the same thing. What are some ways to stop this, but still have access to the variable. Thanks, GEORGE I have an array of products. The array has an ID and price. I'd like to know the most efficient way of adding up all the same ID fields in the array. Code: [Select] <?php $items[] = array( 'id' => 1, 'price' => 5.99, ); $items[] = array( 'id' => 2, 'price' => 1.99, ); $items[] = array( 'id' => 3, 'price' => 2.40, ); $items[] = array( 'id' => 2, 'price' => 6.99, ); $items[] = array( 'id' => 4, 'price' => 8, ); $items[] = array( 'id' => 2, 'price' => 3, ); $items[] = array( 'id' => 3, 'price' => 1, ); function add_like_items($items) { //manipulation return $sums; } //$sums would be an array like //$sum[1] = 5.99 //$sum[2] = 11.98 //$sum[3] = 3.40 //$sum[4] = 8 ?> Anybody have suggestions on the best way to go about doing this? Hi Guys, I am trying to get the while function to list four of my items on a row however I have no idea how and every time I try, I fail. Please give me a hand? Here is the code I have so far Code: [Select] if($row['photo'] == "#no_photo") $photo = ('<img src="images/item_pictures/default_item.png" width="150" height="150" alt="Default Item">'); else $photo = $row['photo']; echo "<br /> <br />" ; echo "<br /> <font face='Comic Sans MS, cursive'>Click on an item to view it in full.</font> <br /> <br />"; if($row['photo'] == "#no_photo") $photo = ('<img src="images/item_pictures/default_item.png" width="100" height="100" alt="Default Item">'); else $photo = $row['photo']; echo "<br /> <br />" ; $i = 0; while ($i < $num) { $f1 = "<tr><td><font size='6'><a href='item.php?id=" . mysql_result($result,$i,"item_id") . "'>" . mysql_result($result,$i,"item_name") . "</a></font></td><td> " . $photo . "</td></tr>"; echo "" . $f1 ."" ; $i++; Thanks, Jacbey How to display items like bottom table?
And here is the code
<? print" <table style=\"width:100%\" class=\"tableList\"> <tr> <th style=\"width:35%\">Prize Name</th> <th style=\"width:12%\">Amount</th> <th style=\"width:12%\">Points</th> <th style=\"width:12%\">Available</th> <th style=\"width:12%\">Redeemed</th> <th style=\"width:17%\">Action</th> </tr>"; $giftCardQuery = 'SELECT currency, amount, pointsPrice, instant_gift_cards.id, instant_gift_cards.giftCardName, instant_gift_cards.giftCardImage FROM instant_gift_card_codes INNER JOIN instant_gift_cards ON (instant_gift_card_codes.giftCardId = instant_gift_cards.id) WHERE instant_gift_cards.status = :cardStatus ORDER BY instant_gift_cards.dateCreated DESC'; $giftCard = $db->prepare($giftCardQuery); $giftCard->bindValue(':cardStatus', 'Enabled', PDO::PARAM_STR); $giftCard->execute(); if($giftCard->rowCount() > '0'){ while($giftCardRow = $giftCard->fetch(PDO::FETCH_ASSOC)){ $giftCardsAvailableQuery = 'SELECT count(*) FROM instant_gift_card_codes WHERE currency = :currency AND amount = :amount AND pointsPrice = :pointsPrice AND giftCardId = :id AND status = :status'; $giftCardsAvailable = $db->prepare($giftCardsAvailableQuery); $giftCardsAvailable->bindParam(':currency', $giftCardRow['currency'], PDO::PARAM_STR); $giftCardsAvailable->bindParam(':amount', $giftCardRow['amount'], PDO::PARAM_STR); $giftCardsAvailable->bindParam(':pointsPrice', $giftCardRow['pointsPrice'], PDO::PARAM_STR); $giftCardsAvailable->bindParam(':id', $giftCardRow['id'], PDO::PARAM_INT); $giftCardsAvailable->bindValue(':status', 'Available', PDO::PARAM_STR); $giftCardsAvailable->execute(); $gCardsAvailable = $giftCardsAvailable->fetch(PDO::FETCH_COLUMN); $giftCardsRedeemedQuery = 'SELECT count(*) FROM instant_gift_card_codes WHERE currency = :currency AND amount = :amount AND pointsPrice = :pointsPrice AND giftCardId = :id AND status = :status'; $giftCardsRedeemed = $db->prepare($giftCardsRedeemedQuery); $giftCardsRedeemed->bindParam(':currency', $giftCardRow['currency'], PDO::PARAM_STR); $giftCardsRedeemed->bindParam(':amount', $giftCardRow['amount'], PDO::PARAM_STR); $giftCardsRedeemed->bindParam(':pointsPrice', $giftCardRow['pointsPrice'], PDO::PARAM_STR); $giftCardsRedeemed->bindParam(':id', $giftCardRow['id'], PDO::PARAM_INT); $giftCardsRedeemed->bindValue(':status', 'Redeemed', PDO::PARAM_STR); $giftCardsRedeemed->execute(); $gCardsRedeemed = $giftCardsRedeemed->fetch(PDO::FETCH_COLUMN); if($giftCardRow['giftCardImage']){ $nameOrImage = '<img src="./images/giftcardrewards/'.$giftCardRow['giftCardImage'].'" alt="'.$giftCardRow['giftCardName'].'" title="'.$giftCardRow['giftCardName'].'">'; }else{ $nameOrImage = $giftCardRow['giftCardName']; } if($gCardsAvailable == '0'){ $redeemAction = 'Out of Stock'; } elseif($userInfo['currentPoints'] < $giftCardRow['pointsPrice']){ $needed = $giftCardRow['pointsPrice'] - $userInfo['currentPoints']; $redeemAction = 'You need '.$needed.' point(s)'; } elseif($userInfo['currentPoints'] >= $giftCardRow['pointsPrice']){ $redeemAction = '<input type="button" value="Redeem" onclick="if(confirm(\'Are you sure to redeem this prize?\')){location.href=\'index.php?do=instantGiftCards&action=redeem&cardId='.$giftCardRow['id'].'&amount='.$giftCardRow['amount'].'\';}">'; } print" <tr> <td>".$nameOrImage."</td> <td style=\"text-align:center\">".$giftCardRow['currency'].$giftCardRow['amount']."</td> <td style=\"text-align:center\">".$giftCardRow['pointsPrice']."</td> <td style=\"text-align:center\">".$gCardsAvailable."</td> <td style=\"text-align:center\">".$gCardsRedeemed."</td> <td style=\"text-align:center\">".$redeemAction."</td> </tr>"; } }else{ print" <tr> <td colspan=\"4\" style=\"text-align:center;color:#2B1B17;padding:15px 0\">No prizes added.</td> </tr>"; } print" </table>"; ?> I would like to add another count that after every 4 items, it loops and create a added piece of script, by adding <ul><li> tags after every 4 items. I want to addthis piece of code, every fours items after it close </div> Code: [Select] <ul> <li> <div class="box"> <!--All fields and information here--> </div> <div class="h2"></div> <div class="box"> <!--All fields and information here--> </div> <div class="h2"></div> <div class="box"> <!--All fields and information here--> </div> <div class="h2"></div> <div class="box"> <!--All fields and information here--> </div> <div class="h2"></div> </li> </ul> I currently have this piece of code, which builds a list per every <div class="box">, then closes the item </div> and start another one as same. SEE current CODE. Code: [Select] <?php $dealList = $this->dealList; if(count($dealList)==0) echo '<h3>'.JText::_('NO_DEAL_MESSAGE').'</h3>'; $count = 1; foreach ( $dealList as $row): $link = 'index.php?xxx=' . $row->id; if(!Helper::is_urlEncoded($row->pic_dir)) { $imageUrl = $row->pic_dir; } else { $imageUrlArr= unserialize(urldecode($row->pic_dir)); $imageUrl = str_replace("\\","/",$imageUrlArr[0]); } ?> <div class="box"> information fields here </div> <?php if ($count%2==1){?> <div class="h2"></div> <?php } else if($count%2==0){?> <div class="h3"></div> <?php }$count++ ; endforeach;?> How do I added the Additonal count for efter four items <div class=box"> it places and start over with Code: [Select] <ul> <li> <div class="box"> information fields </div> </li> </ul> I am trying to do something like this but the code I have doesn't seem to do what I want... Code: [Select] <?php while($item_rows = mysql_fetch_array($item_result)) { $output = " <tr bgcolor=\"#000000\"> <td align=\"center\"><p><img src=\"../images/".$item_rows['item_id'].".gif\" width=\"45\" height=\"45\" /></p> <p>".$item_rows['name']."<br /> Attack: +".$item_rows['attack']."<br /> <img src=\"../images/credit.gif\" width=\"17\" height=\"17\" />$".$item_rows['sell_price']."</p> <p>[Equipt|Destory]<br /> </p> </td>"; $remainder = $i % 5; echo ($remainder == 0 ? ($i == 0 ? "" : "\n</tr>")."\n$output " : $output); } $colspan = (4 - $remainder); $last_row = $colspan ? " <td ".($colspan? "colspan=$colspan" : "")."></td>" : ""; echo $last_row. " </tr> </table>"; ?> My users table on my forum is what gets checked everytime a user refreshes to authentic them.
When killing a monster, I want users to be able to grab items and it will insert them into their inventory. I have this part done already. But I want them to have a plethora of loot available that drops from a mob, and once they click the specific item it will then be inserted into their inventory (server database).
I'm storing the Temporary Item Data in a PHP session variable. Once they kill a monster, the Tempory Item Data variable get's filled with the specific loot and then the user will have the option to choose what items they want to go into their inventory.
My problem is, if they open the game in a new browser they will get a new session id. Since I'm authenticating them through my users table, can I just make a new column called session_id and just use php's session_id() before every session start so no matter which browser they're on they will have the same session right?
You might think, well why dont you just store the temporary item data in a mysql field or rows instead? I want to try to minimize mysql usage as much as possible, as players will be CLICKING a lot to kill mobs, it is most likely very mysql demanding as well and I want to be intuitive about it. I just want to use temporary session data for the loot. Then once the user clicks an item they want, it is EXTRACTED from their tempory item data variable, then I will use MYSQL to insert those items into their inventory.
Is this a fair and intuitive way to do temporary data for item loot? For example, in action RPG's like Path of Exile you kill a group of mobs and you see a shit ton of loot on the floor. (I imagine that loot is just temporary waiting for someone to pick it up right?) Once you do pick it up, mysql is then called to save it right? That's the same logic I have with my web based game. Is using a session variable to store that temporay loot.
Is this an intuitive way to do this, or are there other ways?
Edited by Monkuar, 24 November 2014 - 01:24 AM. How can I check if an array contains two items that are the same? For example ("apples", "bananas", "oranges") returns false But ("apples", "bananas", "bananas") returns true Right now I have a link list a-z and 0-9. you click one of those links and your taken to page listing all items that start with that character. currently I'm using: Code: [Select] SELECT * FROM my_table where LEFT(`my_item`,1)='$show' ORDER BY my_title ASCall I do right now is just add a link like ?show=a and everything that starts with a is listed. Now here is my problem. I want to listen all items that start with a numeric value listed all on one page instead of being spread across 0-9. If another solution becomes available I'd be more then happy to hear you out. But does anyone know how I can change my query to only list items that start with numbers? |