Jump to content

PHP/MYSQL/NEWS/table building help please


godsent

Recommended Posts

I'm started to learn mysql and trying to use my lessons on website.

 

Here are only one entry in my mysql table.(id, type, info, additional, more, date). I'm sure I'm building page wrong way.

 

<? 
include '_client/config.php'; 
include '_client/connect.php'; 
$query = "SELECT * FROM data_base WHERE id='1'";
$res = mysql_query($query);
$arr = mysql_fetch_row($res);

?>
	<div id="primarycontent">

		<!-- primary content start -->

	<div class="post">
	<div class="header">
	<h3>Home <?  print " > ".$arr[2]; print " (id: ".$arr[0].")"; ?>   </h3>
	<div class="date">Updated: <? echo $arr[6] ?></div>
	</div>
	<div class="content">	
				<img src="images/pic1.jpg" class="picA floatleft" alt="" />
				<p><strong><? echo $arr[1] ?></strong></p>
				<? echo $arr[3] ?>
				<p> </p>
				<p> </p>
				<p><? echo $arr[4] ?></p>
		  </div>			
			<div class="footer">
				<ul>
					<li class="printerfriendly"><a href="#">Printer Friendly</a></li>
					<li class="comments"><a href="#">Comments (18)</a></li>
					<li class="readmore"><a href="#">Read more</a></li>
				</ul>
			</div>
		</div>

		<div class="post">		  </div>

		<!-- primary content end -->

	</div>

 

I think I'm doing this wrong way. I'm retrieving all information in arrays (for example $arr[6] is date, $arr[3] is info etc...).

 

What I'm trying to do is how to build this code for every ID in the table? I'm confused about this, please explain me how it works and if you can help me

Link to comment
https://forums.phpfreaks.com/topic/137081-phpmysqlnewstable-building-help-please/
Share on other sites

$query = "SELECT * FROM data_base";  //select all IDs
$res = mysql_query($query);
while($arr = mysql_fetch_row($res)) {
  //code in here will be repeated for each row returned by your query, for example:
  echo "{$arr['id']}: {$arr['type']}<br/>\n";
}

i get it, but how should i echo all this code

 

      <div class="post">
      <div class="header">
      <h3>Home <?  print " > ".$arr[2]; print " (id: ".$arr[0].")"; ?>   </h3>
      <div class="date">Updated: <? echo $arr[6] ?></div>
      </div>
      <div class="content">   
               <img src="images/pic1.jpg" class="picA floatleft" alt="" />
               <p><strong><? echo $arr[1] ?></strong></p>
               <? echo $arr[3] ?>
               <p> </p>
               <p> </p>
               <p><? echo $arr[4] ?></p>
           </div>         
            <div class="footer">
               <ul>
                  <li class="printerfriendly"><a href="#">Printer Friendly</a></li>
                  <li class="comments"><a href="#">Comments (18)</a></li>
                  <li class="readmore"><a href="#">Read more</a></li>
               </ul>
            </div>
         </div>

 

 

even if i echo 'all'; php side wont work

$query = "SELECT * FROM data_base";  //select all IDs
$res = mysql_query($query);
while($arr = mysql_fetch_row($res)) {
  //code in here will be repeated for each row returned by your query, for example:
  echo "{$arr['id']}: {$arr['type']}<br/>\n";
?> 
    <p><?php echo $arr['id'];?>: This HTML will also be repeated for each row in query</p>
  <?php
}

Nice to hear that.

 

Just one more thing: Dont use short tags ( <? ?> ). Always use full php tags (<?php ?>). Short tags are deprecated, are disabled by default in PHP5 and will not be available in PHP6. And what's most important they cause all sorts of problems, when mixing PHP and XML.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.