PHP动态链接问题 - 需要解释

时间:2016-02-01 09:53:42

标签: php

首先,我想说我是php新手。

我正在为虚构的音乐商店创建一个php网站。我创建了一个数据库,我已经使用php从数据库中将所有信息都插入到网站上。在我的项目中,我有三个链接,分别是“主页,关于我们,联系我们”。我已使用此代码在我的网站中插入这三个链接

<?php 
$id = $_GET["id"];
$query = mysql_query ("SELECT id,menu_title,target FROM menu_titles");
while($result = mysql_fetch_assoc($query)){
$id = $result["id"];
$title = $result["menu_title"];
$target = $result["target"];
echo "<li><a href='index.php?id=$id&title=$title&target=$target'>$title</a></li>";
}
?>

上面的代码将遍历我的数据库并获取链接。

我的数据库中的表有3列,分别是“id,menu_title,target”。

我添加了“目标”列,以便与$ _GET一起使用,因此它会向我显示不同的内容。

此外,我创建了about_us.php文件,以便在按下“关于我们”链接时将用户带到该页面。

我的问题是如何更改网站的链接以显示有关该特定链接的内容?例如“Home”show index.php“About Us”仅显示about_us.php内容,“Contact Us”显示contact_us.php内容

Index.php工作正常,我遇到的问题是其他链接内容。

请解释我如何使用链接工作并在按下关于我们的链接时打开about_us.php文件,联系我们也是如此。

感谢。

<?php
error_reporting(E_ERROR | E_PARSE);
include ("includes/layouts/header.php");
include ("includes/database/db.php");
$id = $_GET["id"];
$target = $_GET["target"];
?>
<!-- START OF THE MAIN SLIDER -->
<div class='container-fluid col-xs-12'>
<div class='carousel_bg'>
<div id='carousel' class='owl-carousel owl-theme'>
<img src='images/slideshow_imgs/adele_slideshow.jpg' alt='adele_photo'>
<img src='images/slideshow_imgs/multiple_covers_slideshow.jpg' alt='multiple_music_covers_photo'>
<img src='images/slideshow_imgs/oranged_up_slideshow.jpg' alt='oranged_up_photo'>
<img src='images/slideshow_imgs/random_covers_slideshow.jpg' alt='random_music_covers'>
<img src='images/slideshow_imgs/sean_tizzle_slideshow.jpg' alt='sean_tizzle_photo'>
</div>
</div>
</div>
<!-- END OF MAIN SLIDER -->

<!-- MAIN SECTIONS STARTS -->
<!-- TOP SECTION STARTS HERE -->

<section>
<div class='container dark_grey_color cont_space'>
<div class='row dark_grey'>
<div class='title'>
<?php
$query = mysql_query ("SELECT id,title FROM sections_titles WHERE id  = 1");
$result = mysql_fetch_assoc($query);
if (isset($_GET["id"]))
$id = $result["id"];
$title = $result["title"];
echo "<h3>$title</h3>";
?>
</div> 
<div class='devider'>
</div>
</div>


<div class='row'>
<?php
$query = mysql_query ("SELECT * FROM top_albums_info ORDER BY id DESC LIMIT 4");
while($result = mysql_fetch_assoc($query)){
$album_name = $result["album_name"]; 
$img = $result["image"]; 
$artist = $result["artist"]; 
$company = $result["company"]; 
$genre = $result["genre"]; 
$price = $result["price"]; 
$buy = $result["buy_now"]; 
$id = $result["id"]; 
echo "<div class='col-md-3 col-xs-6'>
<a href ='index.php?id=$id&artist=$artist&company=$company&genre=$genre&price=$price' class='box_link_hover'><div class='box'>
<div class='inside_box'>
<div class='small_title'>
<h4>$album_name</h4>
</div>
<div class='photo_box'>
<img src=$img class='img_dim'>
</div>
<div class='info'>
<p>Artist: $artist</p>
<p>Company: $company</p>
<p>Genre: $genre</p>
<p>Price: $$price</p>
</div>
</div>
<div class='buy_now'>
<p>$buy</p></a>
</div>
</div>
</div>";
}
?>
</div>
</div>
<!-- TOP SECTION END HERE -->

<!-- MIDDLE SECTION STARTS HERE -->

<div class='container light_grey_color cont_space'>
<div class='row dark_grey'>
<div class='title'>
<?php
$query = mysql_query ("SELECT id,title FROM sections_titles WHERE id  = 2");
$result = mysql_fetch_assoc($query);
if (isset($_GET["id"]))
$id = $result["id"];
$title = $result["title"];
echo "<h3>$title</h3>";
?>
</div>
<div class='devider'>
</div>
</div>

<div class='row'>
<?php
$query = mysql_query ("SELECT * FROM middle_albums_info ORDER BY id DESC LIMIT 4");
while($result = mysql_fetch_assoc($query)){
$album_name = $result["album_name"]; 
$img = $result["image"]; 
$artist = $result["artist"]; 
$company = $result["company"]; 
$genre = $result["genre"]; 
$price = $result["price"]; 
$buy = $result["buy_now"];
$id = $result["id"];
echo "<div class='col-md-3 col-xs-6'>
<a href ='index.php?id=$id&artist=$artist&company=$company&genre=$genre&price=$price' class='box_link_hover'><div class='box'>
<div class='inside_box'>
<div class='small_title'>
<h4>$album_name</h4>
</div>
<div class='photo_box'>
<img src=$img class='img_dim'>
</div>
<div class='info'>
<p>Artist: $artist</p>
<p>Company: $company</p>
<p>Genre: $genre</p>
<p>Price: $$price</p>
</div>
</div>
<div class='buy_now'>
<p>$buy</p></a>
</div>
</div>
</div>";
}
?>

</div>
<!-- end of row -->
</div>
<!-- END OF MIDDLE SECTION CONTAINER -->

<!-- BOTTOM SECTION STARTS HERE -->
<div class='container dark_grey_color cont_space'>
<div class='row dark_grey'>
<div class='title'>
<?php
$query = mysql_query ("SELECT id,title FROM sections_titles WHERE id  = 3");
$result = mysql_fetch_assoc($query);
if (isset($_GET["id"]))
$id = $result["id"];
$title = $result["title"];
echo "<h3>$title</h3>";
?>
</div>
<div class='devider'>
</div>
</div>
<div class='row'>


<?php
$query = mysql_query ("SELECT * FROM bottom_albums_info ORDER BY id DESC LIMIT 4");
while($result = mysql_fetch_assoc($query)){
$album_name = $result["album_name"]; 
$img = $result["image"]; 
$artist = $result["artist"]; 
$company = $result["company"]; 
$genre = $result["genre"]; 
$price = $result["price"]; 
$buy = $result["buy_now"];
$id = $result["id"];    
echo "<div class='col-md-3 col-xs-6'>
<a href ='index.php?id=$id&artist=$artist&company=$company&genre=$genre&price=$price' class='box_link_hover'><div class='box'>
<div class='inside_box'>
<div class='small_title'>
<h4>$album_name</h4>
</div>
<div class='photo_box'>
<img src=$img class='img_dim'>
</div>
<div class='info'>
<p>Artist: $artist</p>
<p>Company: $company</p>
<p>Genre: $genre</p>
<p>Price: $$price</p>
</div>
</div>
<div class='buy_now'>
<p>$buy</p></a>
</div>
</div>
</div>";
}
?>
</div>
<!-- end of row -->
</div>

<!-- SMALL SLIDER STARTS HERE -->

<div class='container'>
<div class='carousel_bg'>
<div class='row thumbs'>
<div class='title'>
<h3>UPCOMING SONGS</h3>
</div>
<div class='devider'>
</div>
<div class='col-md-1'></div>
<div class='col-md-10'>
<div id='carousel_thumb' class='owl-carousel owl-theme'>
<img src='images/slideshow_small_imgs/bob_marley.jpg' alt='bob_marley_photo'>
<img src='images/slideshow_small_imgs/coldplay.jpg' alt='coldplay_photo'>
<img src='images/slideshow_small_imgs/music_covers.jpg' alt='random_covers'>
<img src='images/slideshow_small_imgs/the_neverclaim.jpg' alt='the_neverclaim_photo'>
<img src='images/slideshow_small_imgs/twenty_one_pilots.jpg' alt='twenty_one_pilots_photo'>
</div>
</div>
<div class='col-md-1'></div>
</div>
</div>
</div>
<!-- END OF SMALL SLIDER CONTAINER -->
<!-- SMALL SLIDER STARTS HERE -->
</section>
<?php
include ("includes/layouts/footer.php");
?>

1 个答案:

答案 0 :(得分:0)

您需要include index.php内的$target.php页面文件。

这将在index.php中将while()的内容嵌入到您选择的任何位置。我建议将其置于生成链接的if($target == "index"){ //dont include a page here as you're already in index and you don't want the page to include itsself. } else { include($target.".php"); } 循环下方。

$id

axiac的问题评论中提到的一些内容也值得注意。

  1. 自PHP 5.5起,不推荐使用mysql_扩展,自PHP 7.0起删除。更改扩展名很简单,您应该使用其中一个指向文档的链接。 mysqli_PDO mysql

  2. $id = $_GET["id"];$title在您的代码中是多余的。您只使用数据库表行的$targetcontent属性。

  3. 编辑:特定于您的索引页面,您需要将index.php indexcontent.php移动到另一个文件index.php。这样可以使主要内容与网页的状态部分分开,例如导航和内容区域。

    或者,您可以将内容保留在index.php内,并将if语句包含在我在下面分隔的内容中。

    然后您将拥有以下内容:

    <?php error_reporting(E_ERROR | E_PARSE); include ("includes/layouts/header.php"); include ("includes/database/db.php"); $id = $_GET["id"]; $target = $_GET["target"]; ?> <!-- START OF THE MAIN SLIDER --> <div class='container-fluid col-xs-12'> <div class='carousel_bg'> <div id='carousel' class='owl-carousel owl-theme'> <img src='images/slideshow_imgs/adele_slideshow.jpg' alt='adele_photo'> <img src='images/slideshow_imgs/multiple_covers_slideshow.jpg' alt='multiple_music_covers_photo'> <img src='images/slideshow_imgs/oranged_up_slideshow.jpg' alt='oranged_up_photo'> <img src='images/slideshow_imgs/random_covers_slideshow.jpg' alt='random_music_covers'> <img src='images/slideshow_imgs/sean_tizzle_slideshow.jpg' alt='sean_tizzle_photo'> </div> </div> </div> <!-- END OF MAIN SLIDER --> <?php if($target == "index"){ include("indexcontent.php"); } else { include($target.".php"); } ?> <?php include ("includes/layouts/footer.php"); ?>

    indexconent.php

    <!-- MAIN SECTIONS STARTS --> <!-- TOP SECTION STARTS HERE --> <section> <div class='container dark_grey_color cont_space'> <div class='row dark_grey'> <div class='title'> <?php $query = mysql_query ("SELECT id,title FROM sections_titles WHERE id = 1"); $result = mysql_fetch_assoc($query); if (isset($_GET["id"])) $id = $result["id"]; $title = $result["title"]; echo "<h3>$title</h3>"; ?> </div> <div class='devider'> </div> </div> <div class='row'> <?php $query = mysql_query ("SELECT * FROM top_albums_info ORDER BY id DESC LIMIT 4"); while($result = mysql_fetch_assoc($query)){ $album_name = $result["album_name"]; $img = $result["image"]; $artist = $result["artist"]; $company = $result["company"]; $genre = $result["genre"]; $price = $result["price"]; $buy = $result["buy_now"]; $id = $result["id"]; echo "<div class='col-md-3 col-xs-6'> <a href ='index.php?id=$id&artist=$artist&company=$company&genre=$genre&price=$price' class='box_link_hover'><div class='box'> <div class='inside_box'> <div class='small_title'> <h4>$album_name</h4> </div> <div class='photo_box'> <img src=$img class='img_dim'> </div> <div class='info'> <p>Artist: $artist</p> <p>Company: $company</p> <p>Genre: $genre</p> <p>Price: $$price</p> </div> </div> <div class='buy_now'> <p>$buy</p></a> </div> </div> </div>"; } ?> </div> </div> <!-- TOP SECTION END HERE --> <!-- MIDDLE SECTION STARTS HERE --> <div class='container light_grey_color cont_space'> <div class='row dark_grey'> <div class='title'> <?php $query = mysql_query ("SELECT id,title FROM sections_titles WHERE id = 2"); $result = mysql_fetch_assoc($query); if (isset($_GET["id"])) $id = $result["id"]; $title = $result["title"]; echo "<h3>$title</h3>"; ?> </div> <div class='devider'> </div> </div> <div class='row'> <?php $query = mysql_query ("SELECT * FROM middle_albums_info ORDER BY id DESC LIMIT 4"); while($result = mysql_fetch_assoc($query)){ $album_name = $result["album_name"]; $img = $result["image"]; $artist = $result["artist"]; $company = $result["company"]; $genre = $result["genre"]; $price = $result["price"]; $buy = $result["buy_now"]; $id = $result["id"]; echo "<div class='col-md-3 col-xs-6'> <a href ='index.php?id=$id&artist=$artist&company=$company&genre=$genre&price=$price' class='box_link_hover'><div class='box'> <div class='inside_box'> <div class='small_title'> <h4>$album_name</h4> </div> <div class='photo_box'> <img src=$img class='img_dim'> </div> <div class='info'> <p>Artist: $artist</p> <p>Company: $company</p> <p>Genre: $genre</p> <p>Price: $$price</p> </div> </div> <div class='buy_now'> <p>$buy</p></a> </div> </div> </div>"; } ?> </div> <!-- end of row --> </div> <!-- END OF MIDDLE SECTION CONTAINER --> <!-- BOTTOM SECTION STARTS HERE --> <div class='container dark_grey_color cont_space'> <div class='row dark_grey'> <div class='title'> <?php $query = mysql_query ("SELECT id,title FROM sections_titles WHERE id = 3"); $result = mysql_fetch_assoc($query); if (isset($_GET["id"])) $id = $result["id"]; $title = $result["title"]; echo "<h3>$title</h3>"; ?> </div> <div class='devider'> </div> </div> <div class='row'> <?php $query = mysql_query ("SELECT * FROM bottom_albums_info ORDER BY id DESC LIMIT 4"); while($result = mysql_fetch_assoc($query)){ $album_name = $result["album_name"]; $img = $result["image"]; $artist = $result["artist"]; $company = $result["company"]; $genre = $result["genre"]; $price = $result["price"]; $buy = $result["buy_now"]; $id = $result["id"]; echo "<div class='col-md-3 col-xs-6'> <a href ='index.php?id=$id&artist=$artist&company=$company&genre=$genre&price=$price' class='box_link_hover'><div class='box'> <div class='inside_box'> <div class='small_title'> <h4>$album_name</h4> </div> <div class='photo_box'> <img src=$img class='img_dim'> </div> <div class='info'> <p>Artist: $artist</p> <p>Company: $company</p> <p>Genre: $genre</p> <p>Price: $$price</p> </div> </div> <div class='buy_now'> <p>$buy</p></a> </div> </div> </div>"; } ?> </div> <!-- end of row --> </div> <!-- SMALL SLIDER STARTS HERE --> <div class='container'> <div class='carousel_bg'> <div class='row thumbs'> <div class='title'> <h3>UPCOMING SONGS</h3> </div> <div class='devider'> </div> <div class='col-md-1'></div> <div class='col-md-10'> <div id='carousel_thumb' class='owl-carousel owl-theme'> <img src='images/slideshow_small_imgs/bob_marley.jpg' alt='bob_marley_photo'> <img src='images/slideshow_small_imgs/coldplay.jpg' alt='coldplay_photo'> <img src='images/slideshow_small_imgs/music_covers.jpg' alt='random_covers'> <img src='images/slideshow_small_imgs/the_neverclaim.jpg' alt='the_neverclaim_photo'> <img src='images/slideshow_small_imgs/twenty_one_pilots.jpg' alt='twenty_one_pilots_photo'> </div> </div> <div class='col-md-1'></div> </div> </div> </div> <!-- END OF SMALL SLIDER CONTAINER --> <!-- SMALL SLIDER STARTS HERE --> </section>

    +-------------+----------------+---------------------+--------+
    | category_id | name           | parent_category_ids | status |
    +-------------+----------------+---------------------+--------+
    |           1 | New Category   | 1                   |      1 |
    |           3 | ddd            | 1                   |      1 |
    |           4 | test1          | 3                   |      0 |
    |           5 | Test123 recipe | 3                   |      1 |
    |           6 | abceg          | 3                   |      1 |
    |           7 | xyz            | 6                   |      1 |
    +-------------+----------------+---------------------+--------+