PHP - Transpose Rows Into Columns
I am fetching a data from database which comes out like this
EmpID First Name DoJ Supervisor '34698' 'Ian Bobby' '2011-08-09' 'Daniel Lin' '34684' 'Sally Dodd' '2011-08-09' 'James Kirk' '34871' 'Alan Dandy' '2011-08-09' 'Pamela Roy' I want to Transpose it like this EmpID '34698' '34684' '34871' First Name 'Ian Bobby' 'Sally Dodd' 'Alan Dandy' DoJ '2011-08-09' '2011-08-09' '2011-08-09' Supervisor 'Daniel Lin' 'James Kirk' 'Pamela Roy' Please help Similar TutorialsHi, having stopped php for a while, i'm a little rusty, can someone help me - instead of the results returning in a row by row format, can i have it so it returns column by column? so, rather than result 1 being first, then 2 underneath then 3 etc etc, can i have result 2 next to result 1? heres my code $result = mysql_query("SELECT * FROM members ORDER BY member_id DESC") or die(mysql_error()); echo "<table border='2'>"; echo "<tr> <th></th> <th></th> <th></th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row[printf('<img src="%s" height="96" width="128" style="border: 0" />', $row['profile_pic_short'])]; echo "</td><td>"; echo "<a href=\"PHP-Login/advert_pages/view_profile_specific.php?seller_name=$row[login]\">". $row['login'] . "</a>"; echo "</td></tr>"; } echo "</table>"; ?> all help appreciated thanks i'm trying to echo out the results from a query onto a page, but rather than having them echo out as rows in a table, i want them to be displayed in columns, i.e. first column would have the first 10 results and the second column would have the next to. Any ideas on how i could do this? at the moment i'm using this code to display my results $read=mysql_query("SELECT * FROM entry WHERE category_id=".$id." ORDER BY entry_title") or die("query failed".mysql_error()); $result=mysql_num_rows($read); for($j = 0; $j < $result; $j++) { $row = mysql_fetch_array($read); echo "<a href=article.php?id=".$row['entry_id']. ">".$row['entry_title']."</a><br />"; } but this just displays it as a list down the middle thanks Not sure if this is the right forum, but I have a database that I will need to populate with a large number of rows (2000+). I have written a PHP script that uploads individual entries. Is it possible to use something like a spreadsheet where I can set out the rows/columns as they will appear in the database, and then upload in one go rather than uploading each row individually? Thanks for any observations and/or help. Hi, I need help to check multiple rows for many columns, so that the script won't re-insert a duplicate entry. For eg: $isRowExist=mysql_query("SELECT (field1,field2) from table1 WHERE field1='$f1' AND field2='$f2' "); if(mysql_num_rows($isRowExist)==1) print "already in table1!"; else //proceed to insert into table table1 *BUT I tried this and it says mysql_num_rows expects resource, and this warning only shows when I have multipel columns I want to compare (it works just fine with one column in the WHERE clause above) Please help! hello, all: I'm a newbie, and been trying to work out a way so that the results from an array (or mysql recordset for that matter), are nicely aranged in a table. I want them to show in a grid-like manner, with rows and columns added according to number of "items". I worked out this small code snippet, with a sample array, and for the life of me, cant figure out how to make it so it automatically "breaks" columns at 5 and start new row. I got it to show me right number of rows, but then it repeats all 10 names within them, as opposed to just showing 5 names in each row... Appreciate the help... Code: [Select] <?php $names = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia'); $numberColumns = 5; $numberNames = count($names); $numberRows = ceil($numberNames / $numberColumns); echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>"; for ($i=1; $i<=$numberRows; $i++) { echo "<tr>"; foreach ($names as $name) { echo "<td>" . $name . "</td>"; } echo "</tr>"; } echo "</table>"; ?> Hi guys, is there anyway to process this $result from a mysql query inside PHP so that the data below will be formatted to a pivot-like table? The number of rows and columns of the output 'table' will be indefinite. Thanks so much! Data: ID Row Col Name 1 1 A A1 2 2 A A2 3 3 A A3 4 1 B B1 5 2 B B2 6 3 B B3 7 1 C C1 8 2 C C2 9 3 C C3 Results: A1(1) A2(2) A3(3) B1(4) B2(5) B3(6) C1(7) C2( C3(9) Hi. I have a table that I been to summarise, but use its column / files as rows, in otherwords transpose the table.
Structure example is:
id | month | CatA | CatB | CatC | CatD This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=351561.0 I want to echo out a few subcategories per main category. It should be a maximum of 20 entries per column At this stage I have the following... It echo's the results out in 4 columns and not what I want. Any suggestions? $query = "SELECT adsubcat.name AS subname, adsubcat.linkname AS sublink, adcat.clinkname AS clink FROM adsubcat JOIN adcat ON adcat.id=adsubcat.catid WHERE adsubcat.catid='$catid' ORDER BY adsubcat.name ASC"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $count = 0; print '<table style="text-align:left;margin-left:0px;width:943px;">'."\n"; while ($row = mysql_fetch_assoc($result)){ $gal = '<a href="'.$root.'/'.$row['clink'].'/'.$row['sublink'].'">'.$row['subname'].'</a>'; if ($count == 0){ print '<tr>'; } ++$count; print '<td>'.$gal.'</td>'; if ($count == 4){ $count = 0; print'</tr>'."\n"; } } print'</table>'."\n"; } what Im basically trying to do is just like a phpmyadmin function... you select rows you want to update with a checkbox and then it takes you to a page where the rows that are clicked are shown in forms so that you can view and edit info in them... and then have 1 submit button to update them all at once. I have 2 queries that I want to join together to make one row
Is there a way to echo the results of a query as separate columns as opposed to one under the other? Hi all, This application is given me a nightmare. The code fetches root categories and its sub categories from the a database table. The div table have only 2 columns. I want to add 2 extra columns so that I have 4 columns. Because we have a long list of subcategories meaning I have to scroll down to view the category. Please help Code: [Select] {include file='t_javascript_language.tpl'} {set_css} {set_js} <!--<div class="componentheading">{$page_title}</div>--> <div class="adds_man_list"> <span>{$category_pathway}</span> {if $current_category->catname!=""} <div style="border:2px solid #000;"> {include file='elements/display_fields.tpl' position='categoryicon' this_add=$current_category->id} <strong>{$current_category->catname}</strong> <div class="adds_subcat" style="text-align:right;background:#F5F5F5; border:1px solid #FFFFFF; " > {include file='elements/display_fields.tpl' position='categorydetails' this_add=$current_category->id} <span style="font-size:12px;"> {jtext text="ADS_SUBCATEGORIES"}: {$current_category->kids} {jtext text="ADS_ADS"}: {$current_category->nr_ads} <a href="{$current_category->view}" title="{jtext text='ADS_VIEW_LISTINGS'}"> <img src="{$IMAGE_ROOT}view_listings.gif" class="ads_noborder" alt="{jtext text='ADS_VIEW_LISTINGS'}" /> </a> </span> </div> </div> {/if} {if $categories|@count<=0} <h2>{jtext text="ADS_NO_CATEGORIES_DEFINED"}</h2> {/if} <table id="adds_categories" class="ads_table"> {section name=category loop=$categories} {if $smarty.section.category.rownum is odd} <tr> {/if} <td width="50%" class="adsman_catcell" valign="top"> <div class="adds_maincat" {if $categories[category]->watchListed_flag}class="cat_watchlist"{/if}> {include file='elements/display_fields.tpl' position='categoryicon' this_add=$categories[category]->id} <a href="{if $categories[category]->kids>0}{$categories[category]->link}{else}{$categories[category]->view}{/if}" > {$categories[category]->catname} </a> {if $categories[category]->is_new} <!--NEW!!--> <img src="{$IMAGE_ROOT}new.png" alt="new ads" /> {/if} <a href="{$categories[category]->view}" title="{jtext text='ADS_VIEW_LISTINGS'}"> <img src="{$IMAGE_ROOT}view_listings.gif" class="ads_noborder" alt="{jtext text='ADS_VIEW_LISTINGS'}" /> </a> {if $is_logged_in} {if $categories[category]->watchListed_flag} <img src="{$IMAGE_ROOT}watchlist.png" width="16"class="ads_noborder" /> {/if} <a href="{$categories[category]->link_watchlist}"> <img src="{$categories[category]->img_src_watchlist}" width="16" class="ads_noborder" title="{if $categories[category]->watchListed_flag}{jtext text='ADS_REMOVE_FROM_WATCHLIST'}{else}{jtext text='ADS_ADD_TO_WATCHLIST'}{/if}"/> </a> {/if} <br /> </div> <div class="adds_subcat" style="background:#F5F5F5; border:1px solid #FFFFFF; " > {include file='elements/display_fields.tpl' position='categorydetails' this_add=$categories[category]->id} <br /> {section name=subcategory loop=$categories[category]->subcategories} {include file='elements/display_fields.tpl' position='categoryicon' this_add=$categories[category]->subcategories[subcategory]->id} <a href="{if $categories[category]->subcategories[subcategory]->kids>0}{$categories[category]->subcategories[subcategory]->link}{else}{$categories[category]->subcategories[subcategory]->view}{/if}">{$categories[category]->subcategories[subcategory]->catname}</a> {if $categories[category]->subcategories[subcategory]->is_new==1} <img src="{$IMAGE_ROOT}new.png" alt="new ads" /> {/if} ({$categories[category]->subcategories[subcategory]->nr_a} {jtext text="ADS_ADS"}) {if $categories[category]->subcategories[subcategory]->watchListed_flag} <img src="{$IMAGE_ROOT}watchlist.png" width="16" class="ads_noborder" /> {/if} {if $is_logged_in} <a href="{$categories[category]->subcategories[subcategory]->link_watchlist}"> <img src="{$categories[category]->subcategories[subcategory]->img_src_watchlist}" width="14" class="ads_noborder" title="{if $categories[category]->subcategories[subcategory]->watchListed_flag}{jtext text='ADS_REMOVE_FROM_WATCHLIST'}{else}{jtext text='ADS_ADD_TO_WATCHLIST'}{/if}"/> </a> {/if} <br /> <p style="margin-left:25px;"> {include file='elements/display_fields.tpl' position='categorydetails' this_add=$categories[category]->id} </p> {/section} </div> </td> {if $smarty.section.category.rownum is not odd} </tr> {/if} {/section} </table> </div> Many thanks This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=359199.0 I have a page that displays a list of retail locations (est. 100-150 locations). I have my layout setup to fit 3 columns side by side. Ex: <div class="col"></div> <div class="col"></div> <div class="col"></div> .. with the "col" class having a specific width and float: left. Basically, I want to print out the locations in the column and when a column reaches a certain number of locations, it closes that div and starts a new one and continues printing the rest of the locations. How would I go about making sure that once a column has reached a certain number of locations, it closes the div and starts the new one? I have a 2 part question i hope is an easy Fix, i know very little about any of this, I have a list of teams that shows up and is is showing in ONE long column, i want to do to things, Make it multiple columns & sort alphabetically like this: 123 456 789 Here is the link to the test page right now http://tspclan.com/SS/test.php And the page code (top half of it anyways) Code: [Select] <?php require_once("global.php"); $id = htmlentities($_GET['id']); $order = mysql_real_escape_string($_GET['order']); if($order == "levela") { $order = "ORDER BY `defwin` ASC"; $l_pre = "<a href='?id=$id&order=leveld'>"; $l_suf = "</a>"; } elseif($order == "leveld") { $order = "ORDER BY `defwin` DESC"; $l_pre = "<a href='?id=$id&order=levela'>"; $l_suf = "</a>"; } else { $order = "ORDER BY `defwin` DESC"; $l_pre = "<a href='?id=$id&order=levela'>"; $l_suf = "</a>"; } $team = mysql_query( "SELECT * FROM `teams` WHERE `id`='".$id."'"); $t = mysql_fetch_array($team); $team_title = mysql_real_escape_string($t['title']); $t_tag = $t['tag']; $page1 = $t['page1']; $user = mysql_query( "SELECT * FROM `users` WHERE `team`='".$id."' $order"); ?> <script src="http://tspclan.com/js/fbid.js" type="text/javascript"></script> <script src="http://tspclan.com/js/pid.js" type="text/javascript"></script> <script src="http://tspclan.com/js/sorttable.js" type="text/javascript"></script> <title><?php echo $team_title; ?></title> </head> <link href="http://tspclan.com/mkportal/templates/TSP/style.css" rel="stylesheet" type="text/css" /> <body> <div align="center"> <?php if(mysql_num_rows($team) == false) { $team2 = mysql_query( "SELECT * FROM `teams`"); ?> <table width="100%" border="0" class="unsortable" cellspacing="0" cellpadding="0"> <tr> <td background="img/hdr_left.jpg"> </td> <td width="590"><div align="center"><img src="img/hdr.jpg" width="590" height="346" /></div></td> <td background="img/hdr_right.jpg"> </td> </tr> </table> <br><br> <table width="250" cellspacing="0" class="unsortable" cellpadding="5" border="0" id="mwid"> <tr> <td align="center" valign="top" class="index_title unsortable" col span="3" >Team Name</td> </tr> <?php while($t2 = mysql_fetch_array($team2)) { $t_name = "<a style='font-weight:bold; font color:#333333;' href='?id=".$t2['id']."'>".$t2['title']."</a>"; echo " <tr> <td valign='top' align='center' width='50' class='row1 b_t' style='border-bottom:none; width:5px; background-color:#333333;'>".$t_name."</td> "; } ?> 2nd Part of my question, When i call for a team my link is test.php?id=(team id) Is there a way to bring up multiple teams at the same time? I'm working on a site where I've implemented a simple back end wysiwyg editor for content on a page. Then on the public page I run a php query to pull that content and display it. But doing this cancels out the css I have been using to split content into two columns. Is there a way to do this in php, or is there a way to circumvent the problem? ( also tried echoing the entire css style along with the query result - that didn't work either) The <p id='container_sub'> is what is split into two columns. I tried it outside of the query, and inside the query around where I echo results. Neither worked. Here's the basic php code, and further down the css that makes two columns: Code: [Select] <?php $pageid = '2'; // Formulate Query // This is the best way to perform an SQL query // For more examples, see mysql_real_escape_string() $query = sprintf("SELECT content FROM tbl_pages WHERE page_id='%s'", mysql_real_escape_string($pageid)); // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo "<p id='container_sub'>".$row['content']."</p>"; } // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result); ?> The css.... Code: [Select] #container_sub {-moz-column-count: 2; -moz-column-gap: 25px; -webkit-column-count: 2; -webkit-column-gap: 20px; column-count: 2; column-gap: 20px;} #container_sub2 {-moz-column-count: 2; -moz-column-gap: 25px; -webkit-column-count: 2; -webkit-column-gap: 20px; column-count: 2; column-gap: 20px;} I am editing code to get sku numbers, for now it just prints as below: SKU: 123 SKU: 322 SKU: 444 SKU: 555 SKU: 666 I want to put them in columns so it prints as: SKU: 123 SKU: 322 SKU: 444 SKU: 555 SKU: 666 etc. etc. So it is devided into 4 columns. The code that controls is: Code: [Select] <?php if (is_array($order->products)) { $context = array( 'revision' => 'formatted', 'type' => 'order_product', 'subject' => array( 'order' => $order, ), ); foreach ($order->products as $product) { $price_info = array( 'price' => $product->price, 'qty' => $product->qty, ); $context['subject']['order_product'] = $product; $context['subject']['node'] = node_load($product->nid); ?> <?php echo $product->model; ?><br /> <?php } }?> i would appreciate some help i have this code but it shows the password of the user to... Code: [Select] <?php $database="dynamicg_sites"; mysql_connect ("localhost","dynamicg_sites","boom123"); @mysql_select_db($database) or die( "Unable to select database"); $result = mysql_query( "SELECT * FROM sites order by votes DESC" ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); print "<br>\n"; print "There are $num_rows News Articles"; print "<br>\n"; print "<table width=457 border=0>\n"; print "<tr>\n"; print "<td>\n"; echo "Title:"; print "</td>\n"; print "<td>\n"; echo "Description:"; print "</td>\n"; print "<td>\n"; echo "Votes:"; print "</td>\n"; print "</tr>\n"; while ($get_info = mysql_fetch_row($result)){ print "<tr>\n"; foreach ($get_info as $field) print "\t<td><font face=arial size=2/>$field</font></td>\n"; print "</tr>\n"; } print "</table>\n"; print "<br />\n"; print "<br />\n"; ?>i was wondering how do i make this only show certain columns... thanks in advance... Hello - hope you are all well.
I am creating a website for a family friend who has a shop. See preview of the site in this picture ....
Picture1.jpg 40.32KB
0 downloads
Currently the results are showing in one column. I want to show them in three columns in the format of:
1 2 3
4 5 6
7 8 9 etc
But formatted with the picture and then the title and the border.
The current code i have is....
HEAD
<?php
$sql = "SELECT * FROM abs_productcat WHERE catid = $catid ORDER BY catid ASC"; $can = mysql_query($sql); $catname = mysql_fetch_assoc($can); ?> BODY <?php while ($row_pro = mysql_fetch_assoc($rs_proddetails)) { ?><tr> <td><table width="243" border="0" align="left" cellpadding="15" cellspacing="0" class="sectionborders"> <tr> <td height="120" align="center"><p><strong><a href="indproducts.php?ProductID=<?php echo $row_pro['ProductID']; ?>"><img src="products/product_<?php echo $row_pro['ProductID']; ?>" alt="<?php echo $row_pro['ProductName']; ?>" height="120" class="sectionborders" border="0" /></a></strong></p> <p><strong><a href="indproducts.php?ProductID=<?php echo $row_pro['ProductID']; ?>" class="products"><?php echo $row_pro['ProductName']; ?></a></strong></p></td> </tr> </table></td> </tr> <tr> <td> </td> </tr> <?php } ?> THANK YOU FOR YOUR HELP!!! |