[SOLVED] I cant seem to format this php correctly...

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
madeqx
Forum Newbie
Posts: 7
Joined: Wed Jul 28, 2004 11:45 am

[SOLVED] I cant seem to format this php correctly...

Post by madeqx »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color] 


I have two tables, one called country with cid, country_name and another called city with cityid, city_name.

I want to print out the contents of these, categorized by country, like so:

France
 - Paris
 - Nice
 - Cannes

England
 - London
 - Leeds
 - Birmingham

Netherlands
 - Amstedam
 - etc....

To do this I use the following code:

Code: Select all

$query = "SELECT co.country, ci.city FROM country co, city ci WHERE ci.cid=co.cid ORDER BY country";

$result=mysql_query($query); 

while($row=mysql_fetch_array($result)) 
{ 
if($previous_country!=$row['country_name']) 
{ 
echo '<br><b>'.$row['country_name'].'</b><br>'; 
$previous_country=$row['country_name']; 
} 
echo $row['city_name'].'<br>'; 
}
This prints everything out. However, I want to display it like this:
http://www.specialinvestor.com/temp.php

Neater, yet much more difficult (for me) to manage.
In other words, put the contents in this HTML:

Code: Select all

&lt;table width="100%"  border="0" cellspacing="0" cellpadding="1"&gt;
  &lt;tr&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;table width="100%"  border="0" cellspacing="0" cellpadding="0"&gt;
      &lt;tr&gt;
        &lt;td bgcolor="#F4F4FF"&gt;&lt;table width="100%"  border="0" cellspacing="4" cellpadding="0"&gt;
          &lt;tr&gt;
            &lt;td width="80%" align="left" valign="top"&gt;COUNTRY 1 &lt;/td&gt;
            &lt;td width="20%" align="right" valign="top"&gt;FLAG&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td align="left" valign="top"&gt;&lt;ul&gt;
              &lt;li&gt;City 1&lt;/li&gt;
              &lt;li&gt;City 2&lt;/li&gt;
              &lt;li&gt;City ...   &lt;/li&gt;
            &lt;/ul&gt;&lt;/td&gt;
            &lt;td align="right" valign="top"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

Can anyone help me with this?

Cheers


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it looks like you have the basics to do it already.. just need to add the surrounding html into your output to make it appear like the html you posted..

Code: Select all

//...

$country_prefix = '<table width="100%"  border="0" cellspacing="0" cellpadding="1">
  <tr>
    <td bgcolor="#CCCCCC"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td bgcolor="#F4F4FF"><table width="100%"  border="0" cellspacing="4" cellpadding="0">
          <tr>
            <td width="80%" align="left" valign="top">';
$country_intrafix = '</td>
            <td width="20%" align="right" valign="top">';
$flag_suffix = '</td>
          </tr>
          <tr>
            <td align="left" valign="top"><ul>';
$city_prefix = '              <li>';
$city_suffix = '</li>
';
$country_suffix = '            </ul></td>
            <td align="right" valign="top"> </td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
';

$last = '';
$output = '';
while($row = mysql_fetch_assoc($result))
{
  if($last != $row['country_name'])
  {
    if(!empty($output)) $output .= $country_suffix;
    $output .= $country_prefix . $row['country_name'] . $country_intrafix . $row['flag'] . $flag_suffix;
  }
  $output .= $city_prefix . $row['city'] . $city_suffix;
}

if(!empty($output)) $output .= $country_suffix;

//...
madeqx
Forum Newbie
Posts: 7
Joined: Wed Jul 28, 2004 11:45 am

Post by madeqx »

thanks for your help!

Where exactly should i put this code? hehe
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

roughly, it would replace the code you posted.. although you will need to add a flag field, if you want the flag..

it is an example though.. so you will need to alter it some probably.
madeqx
Forum Newbie
Posts: 7
Joined: Wed Jul 28, 2004 11:45 am

Post by madeqx »

ok.

I see you changed mysql_fetch_array to mysql_fetch_assoc. What does this do?

Also, it doesnt print out anything?!
:oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fetch_array returns a numeric and named indices array. fetch_assoc only returns a named indices array. Since you're using using named indicies, I switched it to assoc.. my preference.

As for not printing anything, the output that would be printed is stored in the $output variable. Just echo it when and where you want to..
madeqx
Forum Newbie
Posts: 7
Joined: Wed Jul 28, 2004 11:45 am

Post by madeqx »

thanks for your help! sorted it out now
Post Reply