PHP - Setting # Of Items Per Row From Database...
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>"; ?> 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 I have a php page where I have posts about different topic. I have a menu listing all this categories of topics . Some topics may belong to more than one category. So when i click on a particular menu item, the topic belonging to that category will be displayed. While displaying it is in this format---Topic Heading,Belongs to which all categories,date posted and the description. All this are taken from database. Now my actual issue is I am not able to give links to the categories dispalyed ,if the category exceeds more than 1. For eg: Global Warming Posted on March 11,2011 in News, Issue -----------description of global warming goes here. In the above eg: News and issue are the two categories,when i click on News it should display all the items in news category,and when i click on issue it should display all items of issue category. But I am not able to give this link to the categories. Code: [Select] while($row = mysql_fetch_array($q)) { if($row[6]=='News'){ $c='<a href="category_disp.php?category=news">News</a>'; } if($row[6]=='Issues'){ $c='<a href="category_disp.php?category=issues">Issues</a>'; } echo "<tr><td><p >$row[1]</p><br></td></tr>"; echo "<tr><td> <div >". "<p><span>$row[4]</span> <span>". "<a href='$row[6]' >$row[6]|<a href='$row[5]'> Post Comments</a></span></p>". "<p> </p></div><br></td></tr>"; echo "<tr><td><p >$row[2]</p><br></td></tr>"; echo "<tr><td><p ><a href='$row[3]' >Click Here to read more...</a></p></td></tr>"; echo "</div></td></tr>"; } echo" </table> I want to retrieve items from the DB and seperate them using a comma... while i know how to actually do that, what i want is to automatically have stop adding comas at the last item... so if there are 4 items i want it to show a comma after each item except the last one (1, 2, 3, 4). Does anyone know of a function or can give me an example on how i can do this. I'm trying to make it so that someone can add an item to the shop based upon the current items within the database. How do I go about doing that? I know i have to change <input> to <select>, but beyond that, how do I code it so I can run an array to get all the current items from the table `items` and list them by their `name` field? Thanks in advance! Here is what I have so far: function addshopinv($id) { error_reporting(E_ALL); ini_set('display_errors', 1); if (isset($_POST["submit"])) { extract($_POST); $errors = 0; $errorlist = ""; if ($name == "") { $errors++; $errorlist .= "Name is required.<br />"; } else if ($errors == 0) { $dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>"); mysql_select_db("XXX"); $query = mysql_query("SELECT id FROM items WHERE name='$name'"); while ($result = mysql_fetch_array($query)){ $item_id = $result['id']; $query1 = mysql_query("INSERT INTO sale SET shop_id='$id', item_id='$item_id'"); } admindisplay("Inventory Item Added.","Add New Inventory Item"); } else { admindisplay("<b>Errors:</b><br /><div style=\"color:red;\">$errorlist</div><br />Please go back and try again.", "Add New Item to Shop"); } } $page = <<<END <b><u>Add New Inventory Item</u></b><br /><br /> <form action="admin_panel.php?do=addshopinv:$id" method="post"> <table width="90%"> <tr><td width="20%">Name:</td><td><input type="text" name="name" size="30" maxlength="255" value="" />*255 character max</td></tr> </table> <input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /> </form> END; $page = parsetemplate($page, $row); admindisplay($page, "Add New Inventory Item"); } I coded the following but some parts are not working (the "curveball" mentioned at the end). How would YOU do this? I have a page (A) displaying a form with a textarea field using a WYSIWYG interface. The user will enter a list of unordered items and upon submit the string will look something like this: This is my list of food items: <ul> <li> Lettuce </li> <li> Tomatoes </li> <li> Eggs </li> <ul> Upon submit, I need to give each list item an unique id and store this id along with the text next to it in a separate table: List_item_id List_item_text List_item_state food_1 Lettuce food_2 Tomatoes food_3 Eggs The user will then land in another page with a form displaying the list. The text is now in the page itself and not in a textarea. Instead of the bullets a dropdown list appears, and the user can select "buy" or "sell" for each list item. This page (B) looks something like this: <form> This is my list of food items: <br><select name="food_1"> <option value="buy">Buy</option> <option value="sell">Sell</option> </select> Lettuce <br><select name="food_2"> <option value="buy">Buy</option> <option value="sell">Sell</option> </select> Tomatoes <br><select name="food_2"> <option value="buy">Buy</option> <option value="sell">Sell</option> </select> Eggs <input type=submit value="submit"> </form> When the user hits "submit" the table with the list items will be updated with the values selected in the dropdown list: List_item_id List_item_text List_item_state food_1 Lettuce Buy food_2 Tomatoes Buy food_3 Eggs Sell The next time the user goes to Page B the list will remember the states. Here's the curveball: At any point the user may click on "EDIT LIST" on Page B so they may add more items (at the begining, middle or end of the string). On edit mode the list items should appear as bullets again inside the WYSIWYG interface. Keep in mind that some of the text in the string may not be a list item (ie, "this is my list of food...") This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=334480.0 I've been playing around with this code from http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart Objective: I'm trying to make the scripts work but without pulling the data from MySQL Database. Instead, I created an array and inserted some info there and trying to replicate the same results but getting confused. For example, on the Index.php file, I made the following changes: <?php // Start the session session_start(); // Include functions require_once('inc/functions.inc.php'); // Products multidimensional Array $products = array("book" => array( 'id' => '1', 'title' => 'Learn Your ABC\'s', 'author' => 'John Doe', 'price' => '14.95'), "video" => array('id' => '2', 'title' => 'Visual Guide to learn', 'author' => 'Adam Smith', 'price' => '21.38'), "puzzle" => array('id' => '3', 'title' => 'Can You Solve?', 'author' => 'Sara Brown', 'price' => '9.41'), "exam" => array('id' => '4', 'title' => 'Test Your Knowledge', 'author' => 'Kim Carver', 'price' => '11.15')); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo &#0183; Bookshop</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="booklist"> <h1>Books In Our Store</h1> <?php /* $sql = 'SELECT * FROM books ORDER BY id'; $result = $db->query($sql); $output[] = '<ul>'; while ($row = $result->fetch()) { $output[] = '<li>"'.$row['title'].'" by '.$row['author'].': $'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'; } $output[] = '</ul>'; echo join('',$output); */ ?> <?php $output[] = '<ul>'; foreach ($products as $product => $product_item) { // $flavor is the key and $piece is the value foreach ($product_item as $item => $piece) { if ($item == "id") { # Conditional logic defined to map specific keys to specified name value $row['id'] = $piece; // echo "The id is ". $id."<br />"; // set just to confirm value correct for variable } elseif ($item == "title") { # Conditional logic defined to map 1st key to price value $row['title'] = $piece; // echo "The title is ". $title."<br />"; // set just to confirm value correct for variable } elseif ($item == "author") { # Conditional logic defined to map 1st key to price value $row['author'] = $piece; // echo "The author is ". $author."<br />"; // set just to confirm value correct for variable } elseif ($item == "price") { # Conditional logic defined to map 1st key to shipping value $row['price'] = $piece; // echo "Price: ". $price."<br /><br />"; // set just to confirm value correct for variable } $output[] = '<li>"'.$row['title'].'" by '.$row['author'].': $'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'; $output[] = '</ul>'; echo join('',$output); } } ?> </div> </body> </html> And here's the original index.php before my edits which show the sql query to the db: <?php // Include MySQL class require_once('inc/mysql.class.php'); // Include database connection require_once('inc/global.inc.php'); // Include functions require_once('inc/functions.inc.php'); // Start the session session_start(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo &#0183; Bookshop</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="booklist"> <h1>Books In Our Store</h1> <?php $sql = 'SELECT * FROM books ORDER BY id'; $result = $db->query($sql); $output[] = '<ul>'; while ($row = $result->fetch()) { $output[] = '<li>"'.$row['title'].'" by '.$row['author'].': £'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'; } $output[] = '</ul>'; echo join('',$output); ?> </div> </body> </html> Also, I included the zipped file of the entire script setup. Again, I'm trying to figure out how to make the cart work with using an array for the product instead of MySQL database. My goal is to remove ALL SQL statements from the scripts but stuck with the looping portion to display the title, author, and price from the array as shown above. Hi, What I'm trying to do is create a script that generates a random list of cars whenever it is run. I have a list of available cars in a MySQL database. I'm just not sure how to get php to generate a list of, say, 20 cars, that are randomly picked from the cars in the database. I may also want to add other statistics like MPG (also already in the database). Can anyone show me how to do this? And is it possible to format the returned data in a table or div format? Thanks in advance Unk How can I select the last 5 db items? I was thinking a timestamp or some thing. 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 ! 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.
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! 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? 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> 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 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! 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? 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. Hi, I have this menu code which displays a drop down menu: <ul> <li><a href="<?php echo URL_PUBLIC; ?>">Home</a></li> <?php function submenu($list, $ul){ if($ul){ echo " <ul>";} foreach($list->children() as $newList){ echo ' <li>'.$newList->link($newList->title, (in_array($newList->slug, explode('/', $newList->url)) ? ' class="current"': null)); if ($newList->childrenCount() > 0) { submenu($newList, true); }else{echo "</li>";} } if($ul){echo " </ul></li>";} } submenu($this->find('/'), false); ?> <br style="clear: left" /> </ul> I am trying to exclude certain menu items based on their slug. I have this code, but can't work it into the above: $omit_pages = array('page1', 'page2'); if (in_array($menu->slug, $omit_pages)) { continue; } Does anyone have any ideas how I can work it in? Thanks 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 |