I have a database with products and categories tables. I have managed to show the product category and It's related products. And It's currently looks like following.
But I need this to be done like
Category B
Product 1
Category A
Product 2
Product 3
Product 4
Product 5
Following is my query
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row["name"];?></h1>
<p><?php echo $row["prod_desc"];?></p>
<?php echo $row["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]; ?>
<?php
}
}
?>
Rearrange array first then try to use it for showing data
<?php
$final_array = [];
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$final_array[$row["name"]][] = ['prod_desc'=>$row["prod_desc"],'prod_name'=>$row["prod_name"],'quantity'=>$row["quantity"],'price'=>$row["buy_price"]];
}
?>
<?php foreach($final_array as $key=>$val){?>
<h1><?php echo $key;?></h1>
<?php foreach($val as $v){?>
<p><?php echo $v["prod_desc"];?></p>
<?php echo $v["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]."<br><br>"; ?>
<?php } ?>
<?php } ?>
<?php }?>
Try to store last used category and check if it changed:
<?php
$category='';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
if($category!=$row["name"]) {
?>
<h1><?php echo $row["name"];?></h1>
?>
}
$category=$row["name"];
<?php
<p><?php echo $row["prod_desc"];?></p>
<?php echo $row["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]; ?>
<?php
}
}
?>
Remember to sort output data by category in SQL query
Related
I have created few static blocks just based on the category Id trying to display different blocks .
The problem being some times the block is displayed while some other times it is not .
I guess there is prob with code ? or the way magneto displays static blocks not sure ?
CODE:
<?php
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==14): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==15): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==16): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==18): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==19): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==86): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==25): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==13): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==98): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_fbyz')->toHtml();?>
<?php else: ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_others')->toHtml();?>
<?php endif; ?>
</div>
<?php endif; ?>
please resolve my problem.
If you put this code in product view page then it it not properly way to get current category in product page..
Used Mage::registry("current_category") instead of Mage::getModel('catalog/layer')->getCurrentCategory();
If you come to category directly then you can get current category id product page.then you used to fetch current product categories ids.
$categoryIds = Mage::registry('current_product')->getCategoryIds();
if(Mage::registry("current_category"))
{
$category = Mage::getModel('catalog/category')
->load(Mage::registry("current_category")->getId());
}elseif(is_array($categoryIds )){
//multiple categories id of product. that way i was taken on list category
$category = Mage::getModel('catalog/category')->load($categoryIds[0]);
}
elseif(!is_null($categoryIds)){
$category = Mage::getModel('catalog/category')->load($categoryIds);
}
else{
//no categoies
}
I've got a table called tickets. The Tickets table has 4 columns: user_id, ticket_id, subject, message. I want to loop the ticket_id and subject of the logged in user. I want to echo it with foreach, but I cant get it to work. This is what I've got:
Query:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = '$user_id'") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($getTickets)){
$showTicketID = $row['ticket_id'];
$showTicketSubject = $row['subject'];
}
Code in the page:
<?php foreach ($showTheTickets as $showTheTickets): ?>
<div class="preview">
<?php echo $showTicketID ?>
<?php echo $showTicketSubject ?>
</div>
<?php endforeach; ?>
Anyones willing to help me? Thanks.
Try this:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = $user_id") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_array($getTickets)){
$row = array( 'ticket_id' => $row['ticket_id'], 'subject' => $row['subject'] );
$showTheTickets[] = $row;
}
And then:
<?php foreach ($showTheTickets as $stt): ?>
<div class="preview">
<?php echo $stt['ticket_id'] ?>
<?php echo $stt['subject'] ?>
</div>
<?php endforeach; ?>
Hope it helps...
You are overwriting the values you get from the query and your $showTheTickets array remains empty.
You probably want something like:
$showTheTickets = array();
while($row = mysqli_fetch_assoc($getTickets)){
$showTheTickets[$row['ticket_id']] = $row['subject'];
}
and:
<?php foreach ($showTheTickets as $id => $subject): ?>
<div class="preview">
<?php echo $id; ?>
<?php echo $subject; ?>
</div>
<?php endforeach; ?>
Why not simply
while($row = mysqli_fetch_assoc($getTickets)){
?><div class="preview"><?
echo $row['ticket_id'];
echo $row['subject'];
?></div><?
}
you have to save $showTicketID and $showTicketSubject in a array (could be the same array or 2 arrays) and then in the view show the arrays, like this:
while($row = mysqli_fetch_assoc($getTickets)){
$array[$row['ticket_id']] = $row['subject'];
}
and the view:
<?php foreach ($showTheTickets as $ticketid => $ticketsubject): ?>
<div class="preview">
<?php echo $ticketid ?>
<?php echo $ticketsubject ?>
</div>
<?php endforeach; ?>
I have a query that selects 3 rows; but I only want to display the one row at a time, use the data from that row, then move to the next row, then repeat. How would this be accomplished? Table has 4 items in it.
PHP:
<?php
$server = "secret";
$user = "secret";
$pass = "secret";
$db = "secret";
$con=mysqli_connect($server,$user,$pass,$db);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = 'SELECT * FROM news ORDER BY id DESC LIMIT 3';
mysqli_select_db($con, $db);
$query = mysqli_query($con, $sql);
$number = 1;
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
}
mysqli_close($con);
?>
Attempt to display:
<div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post; ?></p>
<?php
$number++;
?>
Output of attempt:
Test
||
7/14/13
News 1
Testing to see if this will work lalalalalla
NOTE: Tried to repeat the attempt to see if it would work, but it displayed same as output but with a second one that was a duplicate except said News 2
Desired output look:
Newest Title | Newest Date
Newest news. Etc. Insert Big Paragraph of news here etc etc.
2nd Newest Title | 2nd Newest Date
2nd Newest news. Etc. Insert Big Paragraph of news here etc etc.
3rd Newest Title | 3rd Newest Date
3rd Newest news. Etc. Insert Big Paragraph of news here etc etc.
The problem is that you are overwriting your variables with each while() loop -
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
}
so when you try to echo $id, $title, $news_date, and $post, you will only get the last row data
you either need to add your html in the while loop -
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
?>
<div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post; ?></p>
<?php
$number++;
}
or save them to an array
$id = array();
$title = array();
$news_date = array();
$post = array();
while ($result = mysqli_fetch_array($query)) {
$id[] = $result['id'];
$title[] = $result['title'];
$news_date[] = $result['news_date'];
$post[] = $result['post'];
}
and then access the array in your display
<div name='title'><?php echo $title[$number-1]; ?></div> || <div name='news_date'><?php echo $news_date[$number-1]; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post[$number-1]; ?></p>
<?php
$number++;
?>
I used [$number-1] as I assume $number is starting at 1, and arrays are 0 based
edit here are ways to echo your html. It would be beneficial to visit the php documentation, as all of this is there. In these I set $number back to 0, but you can change it back to 1, and just change each of the $number->$number-1, and the $number+1->$number
using a for loop - http://php.net/manual/en/control-structures.for.php
<?php
for($number=0;$number<count($title);$number++){
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
}
?>
using a foreach loop - http://php.net/manual/en/control-structures.foreach.php
<?php
foreach($title as $number => $value{
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
}
?>
using a while loop - http://php.net/manual/en/control-structures.while.php
<?php
$number = 0;
while($number<count($title){
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
$number++;
}
?>
each of the above was done by closing out of php, and writing the html, and then opening php again. You could also just stay in php, and echo the html - http://php.net/manual/en/function.echo.php
<?php
for($number=0;$number<count($title);$number++){
echo "<div name='title'>".$title[$number]."</div> || <div name='news_date'>".$news_date[$number]."</div>";
echo "<p>News ".($number+1)."</p>";
echo "<p>".$post[$number]."</p>";
}
?>
Everything works with this code but i want to have a link from product.php back to category and subcategory.
i have 3 parent categories, page1.php, page2.php and page3.php
also for those i have subcategories page1.php?subcat=1,2,3,4 page2.php?subcat=1,2,3,4
This is php code iam using to product.php
if (isset($_GET['id'])) {
$id= $_GET['id'];
$result2 = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$count = mysql_num_rows($result2);
if ($count > 0) {
while($row = mysql_fetch_array($result2)){
$pid = $row["id"];
$pname = $row["name"];
$pprice = $row["price"];
$pnoncategoryid = $row["noncategoryid"];
$pdesc = $row["desc"];
}
// gjeje emrin e categorise dhe nencategorise
$result3=mysql_query("select * from noncategory WHERE ncid=$pnoncategoryid");
while($row=mysql_fetch_array($result3)){
$noncategoryname = $row["ncname"];
$categoryid = $row["categoryid"];
}
$result4=mysql_query("select * from category WHERE cid=$categoryid");
while($row=mysql_fetch_array($result4)){
$categoryname = $row["cname"];
}
//mbaro
At category and subcategory i want to have link:
<h3><?php echo $pname; ?></h3>
<div class="pbr"></div>
<h4>Category:</h4>
<p class="price"><?php echo $categoryname; ?> / <?php echo $noncategoryname; ?></p>
<div class="pbr"></div>
<h4>Description:</h4>
<p class="desc"><?php echo $pdesc; ?></p>
<div class="pbr"></div>
<h4>Price:</h4>
<p class="price"><?php echo $pprice; ?> <?php echo _valuta_ ?></p>
Sorry for my bad english, hope some1 understand me :))
You want to transform text into a hyperlink. Like this?
<?php echo "<a href='category".$categoryid.".php'>".$categoryname."</a>"; ?> /
<?php echo "<a href='category".$categoryid.".php?subcat=".$pnoncategoryid."'>".$noncategoryname."</a>"; ?>
<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
<?php do { ?>
<?php echo $row_Recordset1['Name']; ?>:
<?php echo $row_Recordset1['Text']; ?>
•
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</marquee>
<?php mysql_free_result($Recordset1); ?>
Print a friendly message to the user instead of NULL:
<?php echo (NULL === $row_Recordset1['Text']) ? "No value" : $row_Recordset1['Text']; ?>
As xil3 illustrates, you can also use this pattern (from the docs):
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
The way you have it written right now, $row_Recordset1 will be null the first time it goes into the loop.
I've rewritten it for you:
<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
<?php while($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { ?>
<?php echo (($row_Recordset1['Name'] != null) ? $row_Recordset1['Name'] : 'n/a'); ?>:
<?php echo (($row_Recordset1['Text'] != null) ? $row_Recordset1['Text'] : 'n/a'); ?>
•
<?php } ?>
</marquee>
<?php mysql_free_result($Recordset1); ?>