I have a pretty simple question, but for some reason I am drawing a blank. I have the following code in my view file, and I want to display the results in a two column table, so the first entry would be on the left, the next would be on the right then the next one after that would be below the first row, and eventually I will use the pagination class (haven’t gotten that far yet) For some reason I can not figure out how to get the results to display in a 2 column format… only one. Any help would be greatly appreciated.
Ideally I would like to have 4 columns, but the code below was started with just the idea of 2 columns.
Thanks!
<table>
db->query($sql);
foreach ($query->result() as $row)
{
echo("");
echo("");
echo $row->Title;
echo ("<br/>");
?>
<img name="<?php echo $row->Thumb;?>" src="../uploaded/portfolio/thumbs/<?php echo $row->Thumb;?>" alt="">
<?php
echo("<br/>");
echo $row->DescText;
echo("</td>");
echo("<td>");
// Display next picture here
echo("</td>");
echo("</tr>");
}
?>
../
Your code example is rather confusing, but I think from your description that you're trying to do something like this:
<table>
<tr>
<?php $i = 0; foreach($query->result() as $row): ?>
<?php if ($i % 2 == 0): ?>
</tr><tr>
<?php endif; ?>
<td>
<?php //whatever you want to put in your column goes here; ?>
</td>
<?php $i++; endforeach; ?>
</tr>
</table>
If you want the table to be four rows across, just change the "if ($i % 2 == 0)" to "if ($i % 4 == 0)".
Related
noob problem: i have some issues with a loop in php...here is the code (i used the same methodology for other pages and it works); the code it is supposed to display the names of the products from a order, it works, but it is not showing the very first product , i don't know why :
<?php $i=1; while($row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems)){ ?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <?php echo $row_selectOrderItems['pro_name']; ?> </td>
<td> <?php echo $row_selectOrderItems['pro_price']; ?> </td>
<td> <?php echo $row_selectOrderItems['q']; ?> </td>
<td> <?php echo $row_selectOrderItems['q']*$row_selectOrderItems['pro_price']; ?> </td>
</tr>
<?php $i++; } ?>
and here is the code where i used mysqli_fetch_array before the loop
$query_selectOrderItems = "SELECT *,order_items.quantity AS q FROM orders,order_items,products WHERE order_items.order_id='$order_id' AND order_items.pro_id=products.pro_id AND order_items.order_id=orders.order_id";
$result_selectOrderItems = mysqli_query($con,$query_selectOrderItems);
$row_selectOrderItems=mysqli_fetch_array($result_selectOrderItems);
Does anyone have any idea how should i modify this code? Thank you!
You're reading and ignoring the first record in the results. Consider how your loop works:
while($row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems))
Each iteration calls mysqli_fetch_array, stores the record in $row_selectOrderItems, then uses that to display the record. Then consider what you do before the loop:
$row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems);
You're doing exactly that same thing, but not displaying that first record.
Simply remove that first call to mysqli_fetch_array before the loop.
$row_selectOrderItems=mysqli_fetch_array($result_selectOrderItems);
remove this line, so that, it will not read the 1st result at starting.
Now, when you use it in the while loop, it reads the first line
may be you used mysqli_fetch_array($result_selectOrderItems) before this for loop.
check once
I've got a system that gathers information out of XML files, for the exporter I have been trying to create a system where it gathers the unique imprints from a SQL table then outputs them & uses it for the export query so we can export only what we are after at that time & not everything.
The code I have does the above fine however I've been trying to format it to sit within a table and every 7/8 iterations it starts a new table row however I've been unsuccessful at this point, with 167 rows currently I want it to look a bit better than a massive list (and easier to use).
The code I have currently is the following:
<table>
<form action="exporttrade.php" method="post">
<b>Please Select imprints you wish to Export for <client></b><br />
<?PHP
mysql_connect('server', 'user', 'password');
mysql_select_db("database");
$sql ="SELECT distinct imprint FROM table";
$results= mysql_query($sql);
while( $imprint = mysql_fetch_assoc($results) ):
?>
<tr><td><?php echo $imprint['imprint']; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint['imprint']; ?>"/></td></tr>
<?php endwhile; ?>
I've been attempting to use a counter and modulo function I've seen online to insert code every nth line (in this case will be the ending < /tr> tag. Does anyone here know how you can sucessfully do what I am after? I'm just a bit stumped at how to format this currently :/
Edit - Just a heads up managed to get this working as I wanted it to now used the following:
$imprints=grab_array($sql);
foreach ($imprints as $imprint){
$imprint=$imprint['imprint'];
$counter++;
if($counter % 8 == 0){
echo "</tr><tr>";
}
?>
<td><?php echo $imprint; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint; ?>"/></td>
<?PHP
}
?>
Try this code.
<table>
<form action="exporttrade.php" method="post">
<b>Please Select imprints you wish to Export for <client></b><br />
<?PHP
mysql_connect('server', 'user', 'password');
mysql_select_db("database");
$count = 0;
$sql ="SELECT distinct imprint FROM table";
$results= mysql_query($sql);
while( $imprint = mysql_fetch_assoc($results) ):
if($imprint)
{
count++;
}
while($count <= 8)
?>
<tr><td><?php echo $imprint['imprint']; ?></td><td><input type="checkbox" name="imprint[]" value="<?php echo $imprint['imprint']; ?>"/></td></tr>
<?php
$count = 0; echo '<tr><td> New Row after 8 </td> </tr>'?>
<?php continue ?>
<?php endwhile; ?>
<?php endwhile; ?>
you can use this logic.
I Need to show three records of MySql table in three different column using php.My Query is
SELECT * FROM TABLE1 WHERE Id IN(1,2,3)
I want to show the result as here
How can i Write LOOP for it?like
while(loop condition)
{
//what will go here?
}
UPDATE: First row fields will show in first column of html table and second record fields will display in second column and so on...I am not asking only show three records
OP saying, it's not so simple. But it is.
So, you have 2 ways to do it.
First. In this case, you are loop through on the 3 columns. Fetch the first row. This put all the data into a div. Class name is column_1. Do it for the other 3. Then floating the divs to left to each other.
$i = 1;
while($row = $db->fetch_row()) {
?>
<div class="column_<?php echo $i; ?>">
<div class="picture">
<?php echo $row["image"]; ?>
</div>
<div class="description">
<?php echo $row["desc"]; ?>
</div>
... and so on...
</div>
<?php
$i++;
}
Second one, when you first collect the data about 3 rows, and then put them into a table rows by row.
<?php
while($row = $db->fetch_row()) {
$results[] = $row;
}
?>
<table>
<tr>
<td><?php echo $result[0]['image'] ?></td>
<td><?php echo $result[1]['image'] ?></td>
<td><?php echo $result[2]['image'] ?></td>
</tr>
<tr>
<td><?php echo $result[0]['desc'] ?></td>
<td><?php echo $result[1]['desc'] ?></td>
<td><?php echo $result[2]['desc'] ?></td>
</tr>
</table>
EDIT
I forgot that, there is a third solution. You can just build the table empty, and then you can update the cells with an ajax call with jQuery.
One way of looping through them is foreach()
assuming you have your results in $results array:
foreach($results as $result) {
//create your table
$result['id']; //has the item id
$result['title']; //has item title
//and so on...
}
HERE is a great tutorial on looping through mysql result sets :D (W3Schools)
Another one HERE
To provide a answer to your comment you must understand how HTML tables work...
<tr> = table row
<td> = table data
You are asking for an entire source code, and this is NOT that place, we don't do your job for you, but if you want, you will have to pay me :) and I am not sure that you agree with this :)
HERE is a good and easy to understand tutorial on HTML tables.
while ($data=mysql_fetch_array($rs)) {
}
EDIT: So a guy in this thread says the following:
"All order data in Magento is product-specific. It doesn't store anything about what category the products are in. So, as you have found out, there are no reports available in Magento for that sort of thing."
Is this true? Is what I am trying to do a lost cause?
I am trying to display product categories on Magento's backend Sales > Order > specific order view > Information > Items Ordered grid. This is not the main "Orders grid" that you see when navigating to Sales > Order. I want to alter the more detailed one that you see after clicking on a specific order in that first grid.
Following advice from this thread, I've made a new column and given it the proper title (Categories).
Then, to try to populate that column, I added code from Joost de Valk's solution on this thread to app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml
I feel like I should be close (or not) but currently that code is just returning the word "Array". Any thoughts?
My current code is below. The column in question is the second td - the one that echoes $cats:
<?php $_item = $this->getItem() ?>
// Here is the snippet I pasted in to define variables.
<?php $product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
} ?>
<?php $this->setPriceDataObject($_item) ?>
<tr<?php if (!$this->canDisplayGiftmessage()): ?> class="border"<?php endif; ?>>
<td>
<?php if ($this->canDisplayContainer()): ?>
<div id="<?php echo $this->getHtmlId() ?>" class="item-container">
<?php endif; ?>
<div class="item-text">
<?php echo $this->getColumnHtml($_item, 'name') ?>
</div>
<?php if ($this->canDisplayContainer()): ?>
</div>
<?php endif ?>
</td>
// Here is the column in question.
<td class="a-center"><?php echo $cats ?></td>
<td class="a-center"><?php echo $_item->getStatus() ?></td>
<td class="a-right"><?php echo $this->displayPriceAttribute('original_price') ?></td>
etc, etc, etc ...
$_cat is an object
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
<td class="a-center"><?php echo $cats ?></td>
You need to know which property you want to display
echo $_cat->getXyz();
eg.
echo $_cat->getName();
To see a list of some of it properties try
print_r($_cat->getData());
So it seems that categories are really complicated and certainly beyond my abilities. I figured out a work-around, however ugly. Since I am not using the manufacturer field and have only 4 relevant categories, I am creating "manufacturers" for each of those 4 categories, going through and assigning them to all products, and then calling for manufacturer in the relevant file. The code for manufacturer is as follows:
<?php
$manufacturer = Mage::getModel('catalog/product')->load($_item['product_id'])->getAttributeText('manufacturer');
echo $manufacturer;
?>
Thanks to this post for that snippet and thanks to everyone else for their help.
I'm using CodeIgniter and I want to display a list of cities in multiple columns in html like this where i can just adjust the number of items inside the <ul> tag and then generate another <ul> tag for the next column
----------------
City name 1 City name 5 City name 9
City name 2 City name 6 City name 10
City name 3 City name 7
City name 4 City name 8
-------------------
I dont know how to make it display like that but here's my current code that just display everything in 1 column.
<ul>
<?php
foreach($row_city as $city):
echo "<li>".anchor("#",$city->city_name)."</li>";
endforeach;
?>
</ul>
You don't need to use tables. Simply use this code:
$i = 0;
echo "<ul>";
foreach($row_city as $city):
if ($i != 0 && $i%4 == 0) {
echo "</ul><ul>";
}
echo "<li>".anchor("#",$city->city_name)."</li>";
$i++;
endforeach;
echo "</ul>";
and just float the lists next to each other.
You can try to display it inside a table like that with that html code:
<table>
<tr>
<td>
<ul>
<li>element1</li>
<li>element1</li>
<li>element1</li>
</ul>
</td>
<td>
<ul>
<li>element2</li>
<li>element2</li>
<li>element2</li>
</ul>
</td>
<td>
<ul>
<li>element2</li>
<li>element2</li>
<li>element2</li>
</ul>
</td>
</table>
And then you must update your code to make it work with more column.
Here an example of php code that show that kind of output:
<table>
<?php
echo '<td><ul>';
$j =4;
for($i = 0; $i<10; $i++ ){
if($j==0){
echo '</ul></td><td><ul>';
$j=4;
}
echo "<li>".$i."</li>";
$j--;
}
echo '</ul></td>';
?>
You can adapt it to your problem (try to replace the for with your foreach).