?
Solved

PHP Loop

Posted on 2012-08-22
3
Medium Priority
?
563 Views
Last Modified: 2012-08-22
I have an html website that i need to make dynamic using php, so that all records are read from sql tables

My HTML looks like this:

<li id="page_Portfolio">
						<div class="box">
							<a href="#" data-type="close" class="close"><span></span></a>
							<a href="#" class="prev"><span></span></a>
							<a href="#" class="next"><span></span></a>
							<h2>Latest Works</h2>
							<div id="gallery1">
								<ul>
									<li>
										<a href="#!/page_More"><img src="images/page2_img1.jpg" alt=""></a>
										<p>Magnis disient monteidi culus musulla duiuscgiat males uadase odiokaorbi nuncodio, gravidare atursuctus a lorem. Maecenasen tristique orci ac sem. </p>
										<a href="#!/page_More"><img src="images/page2_img2.jpg" alt=""></a>
										<p>Magnis disient monteidi culus musulla duiuscgiat males uadase odiokaorbi nuncodio, gravidare atursuctus a lorem. Maecenasen tristique orci ac sem. </p>
									</li>
									<li>
										<a href="#!/page_More"><img src="images/page2_img3.jpg" alt=""></a>
										<p>Baecenas tristique orci ac sem. Duis ultricies pharetra magna. Kertac accumsan malesuada orci. Donec sit amet eros. Lorem ipsum dolor sitamet consectet. </p>
										<a href="#!/page_More"><img src="images/page2_img4.jpg" alt=""></a>
										<p>Baecenas tristique orci ac sem. Duis ultricies pharetra magna. Kertac accumsan malesuada orci. Donec sit amet eros. Lorem ipsum dolor sitamet consectet. </p>
									</li>
								</ul>
							</div>
						</div>
					</li>

Open in new window


So basically the portfolio section has 2 records in every <li></li>
What I have so far is 1 record in every <li> and its working great but i have to have two rows from the db in each li.

Below is what I have done so far:

#website portfolio
function get_websites(){
$strSQL="SELECT * FROM cms_media WHERE media_active = 1 and media_gallerytype = 2 ORDER BY media_date DESC";
$objRS=mysql_query($strSQL);
	
	echo '<li id="page_PortfolioWeb">';
						echo '<div class="box">';
							echo '<a href="#" data-type="close" class="close"><span></span></a>';
							echo '<a href="#" class="prev"><span></span></a>';
							echo '<a href="#" class="next"><span></span></a>';
							echo '<h2>Websites</h2>';
							echo '<div id="gallery1">';
							echo '<ul>';
							
	while($row=mysql_fetch_object($objRS)){

 							echo '<li>';
							
							
					
							echo "<a href='#!/page_MorePro?id=".$row->media_id."'>".gd_image($row->media_pic,'cropratio=2:1&width=240&height=120')."</a>";
							echo '<p>'.$row->media_name.'</p>';
							
			
							
							
							echo '</li>';
							
	}
							
							echo '</ul>';
							echo '</div>';
						echo '</div>';
					echo '</li>';

}

Open in new window

0
Comment
Question by:zahimezher
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 38320505
Try this:

$counter = 0;
$LIs = array();
$LI = "";
while($row=mysql_fetch_object($objRS)){

$LI .= "<a href='#!/page_MorePro?id=".$row->media_id."'>".gd_image($row->media_pic,'cropratio=2:1&width=240&height=120')."</a>";
$LI .= '<p>'.$row->media_name.'</p>';

$counter++;
if($counter == 2)
{
  $LIs[] = $LI;
  $LI = "";
  $counter = 0;
}
      }

echo "<li>".implode("</li><li>",$LIs)."</li>";
0
 
LVL 1

Author Closing Comment

by:zahimezher
ID: 38320535
Brilliant! :) Thank you
0
 
LVL 1

Author Comment

by:zahimezher
ID: 38320836
@gr8gonzo
Check this question out it related to what we already started.
Array Issue by:zahi mezher

thanx again
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question