Loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moussa854
Forum Newbie
Posts: 23
Joined: Thu Apr 30, 2009 4:10 pm

Loop

Post by moussa854 »

Hi I am trying to generate a html pages from mysql database and I want to generate links at the footer for the generated pages. In the code I will retrieve all query startng with 'b' and I will get about 4 pages. I tried to use a loop but it did not work :(
appreciate any help.

Code: Select all

$lttr = 'b';
$perpage = 10;
$q = mysql_query("select count(Name) from hotels_csv where Name LIKE '$lttr%'  ")  ;
$row = mysql_fetch_array($q);
$pages = ($row[0] + $perpage - 1) / $perpage;
 
for ($i = 0; $i <= $pages-1; $i++) {
 
$start= $i;
$start*= 10;
$numb=$i+1;
 
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('hotel',$db);
$sql = "SELECT Name,Adress,Phone FROM hotels_csv  where Name LIKE '$lttr%'  order by name limit $start,10  ";
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
$header='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <title>Hotels</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Style-Type" content="text/css" />
        <link href="style.css" rel="stylesheet" type="text/css" />
        <link href="layout.css" rel="stylesheet" type="text/css" />
        <style type="text/css">
        .search_img {behavior:url(iepngfix.htc);}
        </style></head>
        <body id="page7">
        <div id="main">
        <div id="border">
        <!-- header -->
        <div id="header">
        <div><!--Valid flash version 8.0-->
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
                      codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,24"
                      width="766" height="474">
         <param name="movie" value="flash/header_v8.swf?button=5" /> 
         <param name="quality" value="high" />
         <param name="menu" value="false" />
         <param name="wmode" value="opaque" />
         <!--[if !IE]> <-->
         <object data="flash/header_v8.swf?button=5"
                    width="766" height="474" type="application/x-shockwave-flash">
                  <param name="quality" value="high" />
                  <param name="menu" value="false" />
                  <param name="wmode" value="opaque" />
                  <param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer" />
                  FAIL (the browser should render some flash content, not this).
         </object>
         <!--> <![endif]-->
         </object></div></div>
         <!-- content -->
         <div id="content">
         <div class="row_2">;
$page="";
 
while($data = mysql_fetch_assoc($req))
    {
 
        $input='<h5>'.$data['Name'].'</h5><p>&nbsp;&nbsp;&nbsp; '.$data['Adress'].'<br />&nbsp;&nbsp;&nbsp; '.$data['Phone'].'<div class="title_block" style="height: 16px"><img alt="" src="images/7page_title1.gif" /></div>' ;
    $page=$page." ".$input;
    }
 
if ($i==0){
$ourFileName = 'index-'.$lttr.'.html';
}
else{
$ourFileName = 'index-'.$lttr.'-'.$i.'.html';
}
$file = fopen($ourFileName, 'w') or die("can't open file");
 
$page=$header.$page.$link.'<a href="index-'.$lttr.'.html">1</a> | <a href="index-'.$lttr.'-1.html">2</a> | <a href="index-'.$lttr.'-2.html">3</a> | <a href="index-'.$lttr.'-3.html">4</a> | <a href="index-'.$lttr.'-4.html">5</a> | <a href="index-'.$lttr.'-5.html">6</a> | <a href="index-'.$lttr.'-6.html">7</a> | <a href="index-'.$lttr.'-7.html">8</a> | <a href="index-'.$lttr.'-8.html">9</a> | <a href="index-'.$lttr.'-9.html">10</a> | <a href="index-'.$lttr.'-10.html">11</a> | <a href="index-'.$lttr.'-11.html">12</a>
                </div></div></div>
                <!-- footer -->
                <div id="footer">
</div></div></div></body></html>';
fwrite($file,$page);           
fclose($file); 
 
 
mysql_close();
}
 
Last edited by moussa854 on Sat Sep 19, 2009 5:15 pm, edited 1 time in total.
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Loop

Post by Robert07 »

So what happens when you run the code you posted? And how is that different than what you want to happen? Can you be a bit more descriptive?
Regards,
Robert
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: Loop

Post by peterjwest »

It would be helpful if you could show us the small section of PHP which is troubling you, what you've posted is too long for most people to want to look through, also please use [syntax=php]instead of[/syntax][syntax=php].[/syntax]
moussa854
Forum Newbie
Posts: 23
Joined: Thu Apr 30, 2009 4:10 pm

Re: Loop

Post by moussa854 »

I need to get something like

Code: Select all

<a href="index.html">1</a> | <a href="index-1.html">2</a> | <a href="index-2.html">3</a> | <a href="index-3.html">4</a> | <a href="index-4.html">5</a> | <a href="index-5.html">6</a> | <a href="index-6.html">7</a> | <a href="index-7.html">8</a> | <a href="index-8.html">9</a> | <a href="index-9.html">10</a> | <a href="index-10.html">11</a> | <a href="index-11.html">12</a>
 
at the end of the pages equivelent to the number of the total pages '$pages'

Like this:
http://limousinedirectoryworld.com/Mass ... setts.html
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Loop

Post by Robert07 »

Ok, you can use something like this:

Code: Select all

 
$footer='<a href="index.html">1</a> |';
for ($i=1; $i<$pages;$i++) {
  $pgNum=$i+1;
  $footer .= '  <a href="index-'.$i.'.html">'.$pgNum.'</a> |';
}
$footer = substr($footer,0,-1); //take off the trailing |
 
moussa854
Forum Newbie
Posts: 23
Joined: Thu Apr 30, 2009 4:10 pm

Re: Loop

Post by moussa854 »

This worked great, thanks. :)
Post Reply