loop within loop in PHP MySQL - php

I have created two tables in MySql one where blog content saved and another where blog ID and different pics path saved. I have saved more than one pics in one postID. Now I am trying to fetch the pics and blog post at same time but its not working. code below...
blogposts.php
<?php
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT * , upload_data.FILE_NAME from blog_posts LEFT JOIN upload_data ON blog_posts.postID=upload_data.postID ; ");
while($row = mysqli_fetch_array($result))
{?>
<div class="b-slider j-smallslider" data-height="382">
<ul><?php
if (!empty($FILE_NAME)) {
$result = mysqli_query($conn,"SELECT * from upload_data where postID=postID ; ");
while($row = mysqli_fetch_array($result))
{?>
<li data-transition="3dcurtain-vertical" data-slotamount="12">
<img data-retina src="articles/user_data/<?php echo( htmlspecialchars( $row['FILE_NAME'] ) ); ?>">
</li> <?php }} ?>
</ul>
</div>
<div class="b-blog-one-column__info">
Title : <?php echo( htmlspecialchars( $row['postTitle'] ) ); ?>, <?php echo( htmlspecialchars( $row['postCat'] ) ); ?>
<span class="b-blog-one-column__info_delimiter"></span>
Tag : Nllam
<span class="b-blog-one-column__info_delimiter"></span>
<i class="fa fa-comment"></i>12 Comments
</div>
<?php } ?>
The Problem is I am unable to fetch the pics. :(

use this query
$result = mysqli_query($conn,"SELECT blog_posts.* , upload_data.FILE_NAME from blog_posts,upload_data WHERE blog_posts.postID=upload_data.postID");
and then print your results with print_r() function and check its get results or not.

SELECT blog_posts.*, group_concat(upload_data.FILE_NAME) FROM `blog_posts`
LEFT JOIN upload_data on blog_posts.postID = upload_data.postID
GROUP BY blog_posts.postID
Try like this you will get number of post rows and filename concadinated with commas if you have images. then Apply logic to display results. Any help you need. Don't hesitate to ask me.

hope this work;
$post_id = 1337;
$conn = new PDO(......); //get connection
$conn->query('SET group_concat_max_len = 4096;');
$query = "SELECT bp.* ,
(SELECT GROUP_CONCAT(ud.FILE_NAME SEPARATOR ',')
from upload_data ud where ud.postID=bp.postID) as files
FROM blog_posts bp
WHERE bp.postID=:post_id ;";
$result = $conn->prepare($query);
$result->bindParam(":post_id", $post_id);
$result->execute();
//don't really need while for one row but oh well
while($row = $result->fetch()){
$files=$row['files'];
$files = empty($files) ? [] : explode(',', $files);
?>
<div class="b-slider j-smallslider" data-height="382">
<ul>
<?php foreach ($files as $file){?>
<li data-transition="3dcurtain-vertical" data-slotamount="12">
<img data-retina src="articles/user_data/<?php echo( htmlspecialchars( $file ) ); ?>">
</li>
<?php } ?>
</ul>
</div>
<div class="b-blog-one-column__info">
Title : <?php echo( htmlspecialchars( $row['postTitle'] ) ); ?>, <?php echo( htmlspecialchars( $row['postCat'] ) ); ?>
<span class="b-blog-one-column__info_delimiter"></span>
Tag : Nllam
<span class="b-blog-one-column__info_delimiter"></span>
<i class="fa fa-comment"></i>12 Comments
</div>
<?php } ?>

Related

php retrieve specific data onclick from database in a list format

<?php include ("../navbar.php"); ?>
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn,$query);
?>
<div class="container">
<div class="compound_report_card">
<h2>Compound Report</h2>
<div class="card-content">
<?php
//Check If Id Isset.
if(isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET[ "CHIHBT_ID" ] . "</p>";
$result = mysqli_query($conn, $query);
?>
<?php
while($row = mysqli_fetch_assoc($result )) {
?>
<ul>
<li>Canonical smiles : <?php echo $row['smile_notation']; ?> </li>
<li> Molecular weight : <?php echo $row['molecular_weight']; ?></li>
<li> Heavy atoms : <?php echo $row['number_of_heavy_atoms']; ?></li>
<li> Aeromatic heavy atoms : <?php echo $row['number_of_aeromatic_heavy_atoms']; ?></li>
<li> Rotatable bonds : <?php echo $row['number_of_rotatable_bonds']; ?></li>
<li> H bond acceptors : <?php echo $row['number_of_h_bond_acceptors']; ?></li>
<li> H bond donors: <?php echo $row['number_of_h_bond_donors']; ?></li>
<li> Tpsa A**2 : <?php echo $row['tpsa']; ?></li>
</ul>
<?php } ?>
</div>
</div>
</div>
<?php include ("../footer.php"); ?>
so this is my new code and i am displaying data on web from my database and . So what i am trying to do is i have a lot of id's and i want that when i click on any id the data of that id from
database should get displayed. So the current situation is that when i am clicking on one id whole data from that table is getting displayed and i want only one to get displayed. I want onlyspecific data from every id that i click. So the problem i think is in while loop so please see that also
The problem: Your code is getting all records and isn't querying against your selected ID.
What you need to do: Add selected ID in query to get specific result. Like shown below:
Update: From What I understand $_GET["CHIHBT_ID"] is your ID, right? Change this code in your script:
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn, $query);
?>
To
<?php include("../conn.php");
$query = "select * from semi_synthetic";
?>
Then, change the following code:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
$result = mysqli_query($conn, $query);
?>
With this:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"])) {
$query .= ' WHERE CHIHBT_ID = ' . $_GET["CHIHBT_ID"]; // add selected id in query
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
}
$result = mysqli_query($conn, $query); // run query
?>
and that's it.

php and mysql to create tabs with bootstrap 3

enter image description here
How can I make a loop with php where I separate the comments as rounds
All comments for the round 1
<div id="$row['round']">
$row['comments']
</div>
All comments for the round 2
<div id="$row['round']">
$row['comments']
</div>
but I can have many rounds, so I don't know the exact query to use
<div id="chat" class="chat">
<ul class="nav nav-tabs">
<?php
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<li><a data-toggle="tab" href="#menu'.$row['round'].'">'.$row['round'].'</a></li>';
}
?>
</ul>
<div class="tab-content">
<?php
$i=-1;
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<div class="tab-pane '.( $i = $i ? 'active' : '' ).'" id="menu'.$row['round'].'">';
$test = $row['round'];
$stmt2 = $DB->query("SELECT * FROM messages INNER JOIN system_users ON messages.user_id = u_userid WHERE $id = video_id AND $test = round ORDER BY date ASC");
while ($row2 = $stmt2->fetch()) { ?>
<br />
<p><b>round:</b> <?php echo $row2["round"]; ?></p>
<p><b>Message:</b> <?php echo $row2["message"]; ?></p>
<p><b>User:</b> <?php echo $row2["u_username"]; ?></p>
<p><b>time:</b> <?php echo date_format (new DateTime($row2["date"]), 'd|m|Y H:i:s'); ?></p>
<hr />
<?php
}$i =0;
echo '</div>';
}
?>
</div>
I am completely sure this is not the best solution, but it is working,
thanks anyway..

Return wordpress post featured images using Fishpig in Magento

Im trying to get two of the latest posts from Wordpress that include featured images. Need to get the post title, content (character limited) and the featured image. I have this so far, all that is missing is the featured image.
<div class="block block-blog block-recent-posts">
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT `id`, `post_title`,`post_name` ,`post_content`, `comment_count` FROM `wp_posts` WHERE `post_type`='post' ORDER BY `comment_count` DESC LIMIT 10";
$results = $readConnection->fetchAll($query);
?>
<ul>
<?php
$counter = 0;
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'): ?>
<li class="item">
<?php echo $row['post_title'];?>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
</li>
<?php endif; ?>
<?php
if($counter == 2)
{
break;
}
$counter++;
}
?>
</ul>
Try this:
I have updated your query and added another query to get post image url :
<div class="block block-blog block-recent-posts">
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID WHERE pm.meta_key = '_thumbnail_id' AND p.post_type='post' ORDER BY p.post_date DESC LIMIT 10";
$results = $readConnection->fetchAll($query);
?>
<ul>
<?php
$counter = 0;
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='www.yoursite.com/wp-content/uploads/'.($results1[0]['meta_value']);
echo $url; //YOUR URL just set your site name
//So final url YOUR--> SITENAME/wp-content/uploads/pathfromquery
?>
<li class="item">
<?php echo $row['post_title'];?>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
</li>
<?php endif; ?>
<?php
if($counter == 2)
{
break;
}
$counter++;
}
?>
</ul>
You shouldn't really be using raw SQL here. As a work around, couldn't you do either of the following:
1) Select more than 2 posts (eg. 5) and loop through them and display the 2 that have featured images
2) Ensure all posts have featured images and then select the first 2 posts
3) Use a collection of posts and then add your custom SQL to that.
This code will get 2 posts and add the _thumbnail_id to the collection. You can add some code to this to check this field exists (use $posts->load(true) to debug the SQL query and $posts->getSelect()->where() to add the custom filter)
<?php $posts = Mage::getResourceModel('wordpress/post_collection') ?>
<?php $posts->addPostTypeFilter('post') ?>
<?php $posts->addIsViewableFilter() ?>
<?php // Limit the collection to 2 posts. Change this number or remove this line completely to include all posts
<?php $posts->setPageSize(2) ?>
<?php // Adds the _thumbnail_id meta field to the collection ?>
<?php // This can be used for checking in the SQL whether a post has a featured image ?>
<?php $posts->addMetaFieldToSelect('_thumbnail_id') ?>
<?php $posts->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<h2><?php echo $post->getPostTitle() ?></h2>
<?php if ($image = $post->getFeaturedImage()): ?>
<img src="<?php echo $image->getAvailableImage() ?>" alt="" />
<?php endif; ?>
<div class="post-content"><?php echo $post->getPostContent() ?></div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Fishpig get last 3 blog posts

Can anyone supply me with some code that will grab the last 3 blog posts from Wordpress within Magento using the Fishpig extension. Can't find a template to use anywhere. Need to grab blog title, content and featured image.
I believe i'm on the wrong track with what I have so far.
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID ORDER BY p.post_date";
$results = $readConnection->fetchAll($query);
?>
<?php
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='/news/wp-content/uploads/'.($results1[0]['meta_value']);
?>
<div class="blog-post-image">
<img src="<?php echo $url; ?>">
</div>
<div class="blog-post-content">
<?php ?>
<h3> <?php echo $row['post_title'];?></h3>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
More Info
</div>
<?php endif; ?>
<?php
if($counter == 4)
{
break;
}
$counter++;
}
?>
Thanks.

Creating Dynamic Links with PHP/MySQL

i'm creating my first PHP/MySQL site and i'm having difficulty figuring out how to generate dynamic links and creating a new page for those links.
My index page is pulling in certain details from my database as a preview, and when the visitor clicks on that item, i want them to be taken to a page which shows the full information from the database for that row.
The code on my index page for displaying the previews is below, any help on amending it to generate the link and page would be greatly appreciated.
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle"><?php echo $f2; ?></li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
</div>
<?php
$i++;
}
?>
I'm pretty sure what i'm asking is really simple, i just can't get my head around acheieving it.
Thanks to you both for your answers, but i have managed to fix it (or work-around it) with this on my index page:
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
do {?>
<div class = "hjl"><a href="paging.php?job_id=<?php echo $rsjobinfo['job_id'];?>">
<ul>
<li id = "jobtitle"><?php echo $rsjobinfo['job_title'];?></li><br />
<li id = "compname"><?php echo $rsjobinfo['company_name'];?></li>
</ul>
<ul>
<li id = "city"><?php echo $rsjobinfo['city'];?>,
<?php echo $rsjobinfo['country'];?></li>
</ul>
</a>
</div>
<?php } while ($rsjobinfo=mysql_fetch_assoc($result))?>
</div>
Followed by this on my content page:
<?php
$job_id = $_GET['job_id'];
$query="SELECT * FROM $tbl_name WHERE job_id = $job_id";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
?>
Thanks for your help everyone.
Dan
put mysql_close after you use mysql_result, but once you get it working you might look into a more modern approach like PDO.
to your code add link (which I think you already have somewhere):
//...................
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li>
//...................
Read more...
//...................
then your code must check for variable $_GET['id'], so put IF in the beginning of your code:
$where = '';
if( isset($_GET['id']) && strlen($_GET['id']) > 0 ) {
$where = ' job_id = "'. mysql_real_escape_string( $_GET['id'] ) .'"' ;
}
<?php
$query="SELECT * FROM $tbl_name $where ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
Read more...
</div>
<?php
$i++;
}
?>
edit: Try changing the following line:
$where = " job_id = '". mysql_real_escape_string( $_GET['id'] ) ."'" ;

Categories