Jump to content

Linking to Specific Id's


ltoto

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....
Link to comment
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
Link to comment
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
Link to comment
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

Link to comment
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
Link to comment
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?
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.