I have a table in database as "neotheme_blog_post" and there are many post in there, now i want to fetch recent 3 posts from this table and show them on home page: I have tried to fetch the data as follows but nothing worked:
<?php $connection =
Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "Select * FROM 'neotheme_blog_post'";
$rows = $connection->fetchAll($query);
foreach ($rows as $values) {
echo $name = $values['name'];
}?>
You may use LIMIT over here.
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "Select * FROM neotheme_blog_post LIMIT 3";
$rows = $connection->fetchAll($query);
foreach ($rows as $values){
echo $name = $values['name'];
}
In my case i get the three recent posts as like follows using neotheme blog extension:
$connection =
Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "Select * FROM neotheme_blog_post ORDER BY created_at DESC LIMIT 3 ";
$rows = $connection->fetchAll($query);
foreach ($rows as $values) {
$post_titile = $values['cms_identifier'];
echo '<div>';
echo '<h1>' . $name = $values['title'] . '</h1>';
echo $summery = $values['summary'];
echo 'Read More';
echo '</div>';
Related
I try to retrive all rows related to the specific category(continent table) but it only shows one and not all.
<?php
$townid = $_GET['id'];
$query = "SELECT * FROM towns INNER JOIN continents ON towns.catid = continents.id WHERE continents.id = '$townid'";
$row = mysqli_fetch_assoc(mysqli_query($link, $query));
// The category/Continent
echo '<h1>';
echo $row['catname'];
echo '</h1>';
//The post name/Town name
echo $row['title'];
?>
You need to call mysqli_fetch_assoc() in a loop.
$result = mysqli_query($link, $query);
$first = true;
while ($row = mysqli_fetch_assoc($result)) {
if ($first) { // Only show the category header once.
// The category/Continent
echo '<h1>';
echo $row['catname'];
echo '</h1>';
$first = false;
}
//The post name/Town name
echo $row['title'];
}
You can do something like:
<?php
$townid = $_GET['id'];
$query = "SELECT * FROM towns INNER JOIN continents ON towns.catid = continents.id WHERE continents.id = '$townid'";
$rows = mysqli_fetch_all(mysqli_query($link, $query), MYSQLI_ASSOC);
// The category/Continent
foreach ($rows as $row) {
echo '<h1>';
echo $row['catname'];
echo '</h1>';
//The post name/Town name
echo $row['title'];
}
?>
On another note, its better to parameterised your query since you are getting the $townid from a GET params.
This is how to do it in PHP https://www.php.net/manual/en/mysqli-stmt.bind-param.php
i have this php code that fetches images from the database using the userid, then displays all the images in a list form. im trying to paginate the images, in a limit of 5 items per page. but the code is showing only the first page, without a link to the other pages. here's my php code
<?php
include 'connect.php';
$Category = " ";
$query = "SELECT Img_dir, Caption, Category FROM images WHERE Category = '". $_REQUEST['Category'] ."' AND user_id = '". $_SESSION['user_id'] ."' LIMIT 0,5";
$result = mysqli_query($conn,$query);
while ($row=mysqli_fetch_array($result)){
$image = $row["Img_dir"];
$Caption= $row["Caption"];
$Category = $row["Category"];
echo "<dl>";
echo "<dd>$Category    <img src='base64_encode($image)' />   $Caption<dd>";
echo "</dl>";
}
//number of total pages available
$results_per_page = 10;
$number_of_results = mysqli_num_rows($result);
echo $number_of_pages = ceil($number_of_results / $results_per_page);
echo "<br>"; echo "<br>";
for($r=1;$r<=$number_of_pages;$r++)
{
?><?php echo $r." "; ?><?php
}
?>
You can try this:
Change your query (use prepare statments):
$query = "SELECT Img_dir, Category FROM images WHERE user_id = ? AND Category = ? ";
As for the structure of your data.
$results = [];
while ($row = $result->fetch_assoc()){
$key = $row['Category'];
if(!isset($results[$key])) $results[$key] = [];
$results[$key][] = $row['Img_dir ']; //['Category' => [Img_dir,Img_dir, ...]]
}
And your HTML. I would use a description list or dl as it has a nice place for the title:
foreach($results as $Category => $image){
echo "<dl>";
echo "<dt>$Category</dt>";
foreach($data as $row){
echo "<dd><img src='base64_encode($image)' /><dd>";
}
echo "</dl>";
}
Untested.
The order will probably be all wanky, so you can use ksort on it. Simply
ksort($results);
Before the foreach loops.
Cheers.
I am trying to create dynamic menu using the below code, but it takes too much time to load:
<?php
ob_start();
include ('admin/db/db.php');
echo '<li>Exports';
$sql3 = "SELECT * FROM category_master where type = 'Export' order by sequence asc ";
$result3 = mysqli_query($connection, $sql3);
if (mysqli_num_rows($result3) > 0)
{
// output data of each row
echo '<ul>';
while ($userrows3 = mysqli_fetch_assoc($result3))
{
$cid = $userrows3['product_category_id'];
$cname = $userrows3['category_name'];
echo '<li>' . $cname . '';
$sql4 = "SELECT group_product FROM products,category_master,cat_wise_pro where category_master.product_category_id = cat_wise_pro.category_id and products.product_id = cat_wise_pro.product_id and category_master.product_category_id =$cid group by products.group_product ";
$result4 = mysqli_query($connection, $sql4);
if (mysqli_num_rows($result4) > 0)
{
// output data of each row
echo '<ul>';
while ($userrows4 = mysqli_fetch_assoc($result4))
{
$gname = $userrows4['group_product'];
echo '<li>' . $gname . '';
$sql5 = "SELECT product FROM products,group_products where products.group_product = group_products.group_product and products.group_product = '$gname'";
$result5 = mysqli_query($connection, $sql5);
if (mysqli_num_rows($result5) > 0)
{
// output data of each row
echo '<ul>';
while ($userrows5 = mysqli_fetch_assoc($result5))
{
$pname = $userrows5['product'];
echo '<li>' . $pname . '';
}
echo '</ul>';
}
}
echo '</ul>';
}
}
}
echo '</ul>';
?>
should i use view instead of table...find below the view i create
Any ideas how to make it faster?
What your code does is:
categories = load C categories from DB
foreach category:
groups = load G groups from DB
foreach group:
products = load products from DB
which sends 1 + C x G queries to database. What you should do is:
products = load products with groups with categories
$tree = [];
foreach ($products as $p):
$tree[$p['category']][$p['group']][] = $p;
foreach ($tree as $category => $groups):
foreach ($groups as $group => $products):
foreach ($products as $p):
...
Use JOIN to fetch the data at once, e.g. something like:
SELECT p.name as product, g.name as group, c.name as category
FROM product p
JOIN group_product g ON p.group_product = g.id
JOIN category_master c ON g.category = c.id;
I want to make a Navigation with 2 levels.
My Code so far
<?php
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='' ORDER BY name");
$result = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)) {
$list[] = $row;
}
foreach ($list as $kat) {
echo '<li>' . $kat['name'] . '</li>';
}
?>
Nested Sets are at the moment too tricky for me.
I want at the end this.
<li>$kat['name']
<li>$kat['name'] from PID</li>
</li>
MySQL:
http://i46.tinypic.com/35052m0.png - IMG
No I want to get the things our of the MySQL DB see the image Link.
MySQL:
id—–pid——name
1——0——–name1
2——0——–name2
3——0——–name3
4——3——–name3.1
5——3——–name3.2
<?php
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='' ORDER BY name");
$result = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)) {
$list[$row['id']] = $row;
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='".$row['id']."' ORDER BY name");
$res = mysql_query($sql);
while($rw = mysql_fetch_assoc($res)){
$list[$row['id']]['sub'][] = $rw;
}
}
echo "<pre>";
print_r($list);
?>
I'm using the following not well thought code block to retrieve categories and it's topics.
$query1 = "SELECT * from categories";
$result = mysql_query($query1);
while ($out = mysql_fetch_assoc($result)){
//category
print '<h2>' . $out['name'] . '</h2>';
$query2 = "SELECT * from topics where fkid = $out[id]";
$result2 = mysql_query($query2);
while($top = mysql_fetch_assoc($result2)){
//topic
print $top['name'] . '<br>';
}
}
The above does the trick. I know this is not the most practical and this being the reason I ask the group.
How can I better this so it's more practical and simple?
A classical case for a JOIN:
SELECT * FROM categories JOIN topics ON categories.id = topic.fkid ORDER BY categories.name;
Then to print, we only print the header if it has changed (thanks, Rajasur!):
$catname = "";
while ($out = mysql_fetch_assoc($result))
{
if ($out["name"] != $catname) // maybe need strcmp here
{
$catname = $out["name"];
print($catname)
}
/* print the rest */
}
Just use a single query that joins the two tables.
$query = "SELECT categories.name AS category_name, topics.name AS topic_name
FROM categories JOIN topics ON categories.id = topics.fkid
ORDER BY categories.name ASC, topics.name ASC";
$resultSet = mysql_query($query);
$currentCategory = "";
while ($cRecord = mysql_fetch_assoc($resultSet)) {
if ($currentCategory != $cRecord['category_name']) {
$currentCategory = $cRecord['category_name'];
echo "<h2>" . $currentCategory . "</h2>";
}
echo $cRecord['topic_name'] . "<br/>";
}
mysql_close($resultSet);