Jump to content

Archived

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

ltoto

Linking to Specific Id's

Recommended Posts

Couldnt think of the right title for this, anyway...

So i what i have done already is i have a table like this:

tabCountry - countryId, countryName
tabRegion - Id, countryId, regionName
tabHotel - Id, regionId, hotelName, hotelImage, hotelDescription, hotelRating

so what i am doing is a system like the example on this website:
http://212.161.124.21/hotellist.asp

i have the countrys and regions bit working, so there is the countrys listed, and within the countrys there is the list of region withineach country, i used the code below

[code]<?php

$sql="SELECT c.countryName, r.regionName, r.Id FROM tabCountry c, tabRegion r WHERE c.Id = r.countryId";
$result = mysql_query($sql);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
$country = "null";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $cheader = $row['countryName'];
  if (is_null($country) || strcmp($country,$cheader) !=0){
      $country = $row['countryName'];
      echo <<<HTML
        <br>
        <h2>$country</h2>
HTML;
  }
echo <<<HTML
  <a href="../pages/hotels.php?Id=$row[Id]">$row[regionName]</a><br>
HTML;
}
?>[/code]

so when you click on a region, it goes to a new page with the list of hotels from that region, so I am now looking how a link the hotels to the region so that when the click on the region, just the hotels from that region appear, and not all the hotels, hope this makes sense....

Share this post


Link to post
Share on other sites
hotels.php needs the following:

[code]
<?php
$id = $_GET['Id'];

$sql="SELECT * FROM tabHotel WHERE regionId = $id";
$result = mysql_query($sql);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $name = $row['hotelName'];
  $description = $row['hotelDescription'];
  $rating = $row['hotelRating'];
  // List the hotels
  echo "$name - $rating - $description<br>\n"
}
[/code]

Huggie

Share this post


Link to post
Share on other sites
[code]$id = $_GET['Id'];[/code]

this is the bit that is not working now, i did change $id to $Id aswell which didnt seem to work

Share this post


Link to post
Share on other sites
If you right click on the page that has the list of countries and regions and then view the source.  Does the <a href... link look correct?

Does it say something like [code=php:0]<a href="../pages/hotels.php?id=5">Greece</a>
[/code]

Huggie

Share this post


Link to post
Share on other sites
/pages/hotels.php?Id=1

annd the id changes with each one to the correct id

Share this post


Link to post
Share on other sites
OK, so we know it's getting the right id...

So on your hotel.php, if you type this:

[code]
<?php
  $id = $_GET['Id'];
  echo $id;
?>
[/code]

Does it echo the correct values... Try clicking a couple of regions from the previous page.

Huggie

Share this post


Link to post
Share on other sites
ok i will have to get back to you tommorrow, because it is now hometime, thanks for all the help today though

Share this post


Link to post
Share on other sites
it sounds like perhaps what you need then is to do a sql join where you join the hotels to the region so that it will pull only hotels from that region and spit them out to you in a results array....

Share this post


Link to post
Share on other sites
its stopped working again now, basically, when i make hotels as a separte page, its fine, but when it is as an includes and i take the connection of the top it does not work.

Also a quick question, what include would i put on the index page for the hotels page

i would use this normally because iput most things in one page

[code]<?php  include 'pages/page.php'; ?>[/code], but obviously i just want it to appear when they click on the region name

Share this post


Link to post
Share on other sites
Right this is the code at the moment [code]<?php
mysql_select_db($database_conTotal, $conTotal);
$query_rsHotel = "SELECT * FROM tabHotel";
$rsHotel = mysql_query($query_rsHotel, $conTotal) or die(mysql_error());
$row_rsHotel = mysql_fetch_assoc($rsHotel);
$totalRows_rsHotel = mysql_num_rows($rsHotel);

$id = $_GET['Id'];

$sql="SELECT * FROM tabHotel WHERE regionId = $id";
$result = mysql_query($sql);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $name = $row['hotelName'];
  $description = $row['hotelDescription'];
  $rating = $row['hotelRating'];
  // List the hotels
  echo "$name - $rating - $description<br>\n";
}
?>
<?php
mysql_free_result($rsHotel);
?>[/code]

which brings Undefined Variable errors, so Im assuming this means i have done something wrong with the include on the index page

Share this post


Link to post
Share on other sites
OK, still have this code on the index.php (note, I've changed the link at the bottom of the page to call the current page.

[code]
<?php

$sql="SELECT c.countryName, r.regionName, r.Id FROM tabCountry c, tabRegion r WHERE c.Id = r.countryId";
$result = mysql_query($sql);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
$country = "null";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $cheader = $row['countryName'];
  if (is_null($country) || strcmp($country,$cheader) !=0){
      $country = $row['countryName'];
      echo <<<HTML
        <br>
        <h2>$country</h2>
HTML;
  }
echo <<<HTML
  <a href="{$_SERVER['PHP_SELF']}?id={$row['Id']}">{$row['regionName']}</a><br>
HTML;
}
?>
[/code]

Then where you want your list of hotels to appear on your index.php page, have this:

[code]
if (isset($_GET['id']){
  $id = $_GET['id'];
  include('hotel.php');
}
[/code]

and your hotel.php looks like this:

[code]
<?php
$sql="SELECT * FROM tabHotel WHERE regionId = $id";
$result = mysql_query($sql);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $name = $row['hotelName'];
  $description = $row['hotelDescription'];
  $rating = $row['hotelRating'];
  // List the hotels
  echo "$name - $rating - $description<br>\n";
}
?>
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
i forgot to explain something sorry, on the index page, there is an include to pages page like this

[code]<?php  include 'pages/page.php'; ?>[/code]

and on the "page" page, that is where all the functions are which i have shown you before, like the list of hotels and countrys, and page content.

so Im assuming i would need to do something on the index page where,

i put the hotels include on that page, but some how say, only show pages/hotels.php, if the region name is clicked on, on pages/page.php


does that make any sense?

Share this post


Link to post
Share on other sites
It does, but I think you're getting slightly confused.  Once a page is 'included' it becomes part of the page it's being included in.

if I have index.php like this:

[code]
<?php
include('header.php');
echo <<<HTML;
<body>
</body>
<html>
HTML;
?>
[/code]

and header.php looks like this:

[code]
<?php
echo <<<HTML
<html>
<head>
  <title>This is index.php</title>
</head>
HTML;
}
[/code]

The resulting code for index.php is:

[code]
<html>
<head>
  <title>This is index.php</title>
</head>
<body>
</body>
<html>
[/code]

It treats all the includes as 'one page'.  So it's no big deal really which page you have it on, if pages/page.php has all the code on it, then by all means include it on there.  You still have to capture the 'id' from the URL and specify where on the page you want the list of hotels displayed.

Regards
Huggie

Share this post


Link to post
Share on other sites
ok so i put the include on the pages page, but it shows this error

   
Parse error: parse error, unexpected '{' in

which is on this line of the include:

[code]if (isset($_GET['id']){[/code]

Share this post


Link to post
Share on other sites
Change this: [code=php:0]if (isset($_GET['id']){[/code]

to this: [code=php:0]if (isset($_GET['id'])){[/code] <- Notice the extra parenthesis

If this was in the code I wrote, I apologise.

Regards
Huggie

Share this post


Link to post
Share on other sites
ok now there seem tobe something wrong with this line

include('hotel.php');

it says

main(hotel.php): failed to open stream: No such file or directory

so i also tried:

include('pages/hotel.php');

, and also, when i click on a region name it goes onto the index page, but i assume this is the reason why

Share this post


Link to post
Share on other sites
You will need to include the path in the include yes.

As for the error, I'm assuming your include structure is like this...

[b][color=green][pre]
|- index.php -|
              |- include(pages.php) - |
                                      |- include(hotel.php)
[/pre][/color][/b]

So your index.php includes pages.php (which happens to be in a seperate directory named pages) and pages.php includes hotel.php (which is in the same directory as pages.php). Is this correct?

Regards
Huggie

Share this post


Link to post
Share on other sites
i made a misatake, i put hotel.php, but it is hotels .php

but there is still a problem, which is, thi results now appear on the index page, or the home page when you click on the link, instead of  it being on its own page

Share this post


Link to post
Share on other sites
ok, create the following three new pages and tell me if it works as expected:

[b]index.php[/b][code]
<?php
include('connect.php') // This is the name of your db connection file
include('/pages/page.php');
?>
[/code]

[b]pages.php[/b] - In the pages directory[code]
<?php
$sql="SELECT c.countryName, r.regionName, r.Id FROM tabCountry c, tabRegion r WHERE c.Id = r.countryId";
$result = mysql_query($sql);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
$country = "null";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $cheader = $row['countryName'];
  if (is_null($country) || strcmp($country,$cheader) !=0){
      $country = $row['countryName'];
      echo <<<HTML
        <br>
        <h2>$country</h2>
HTML;
  }
echo <<<HTML
  <a href="{$_SERVER['PHP_SELF']}?id={$row['Id']}">{$row['regionName']}</a><br>
HTML;
}
if (isset($_GET['id']){
  $id = $_GET['id'];
  include('hotel.php');
}
?>
[/code]

[b]hotel.php[/b] - In the pages directory along side pages.php[code]
<?php
$sql="SELECT * FROM tabHotel WHERE regionId = $id";
$result = mysql_query($sql);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $name = $row['hotelName'];
  $description = $row['hotelDescription'];
  $rating = $row['hotelRating'];
  // List the hotels
  echo "$name - $rating - $description<br>\n";
}
?>
[/code]

If you then goto index.php you should get a list of countries and their regions listed below.  When you click on one of the regions, index.php should reload with the same list of countries and regions, but also the hotels you selected below them.

Regards
Huggie

Share this post


Link to post
Share on other sites
sorry, just want to ask if you posted this without seeing my post above becuase the er posted around similar times?

Share this post


Link to post
Share on other sites

×

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.