Search code examples
phpmysqlcontent-management-systemhref

href to a database field link.


Im creating my website FOUND HERE

(this page is a result of clicking on the free tab at the bottom of my home page found here)

as you can see on this page there are 2 fields stored on the database. GIFFGAFF & O2. It successfully reads the promo_image, promo_title, promo_content from my mobi table on my database.

However: When you hit the GO button it is supose to link you to the site stored in promo_link in the same table for this offer.

It does not do that. But it loads the same page in a new tab (due to the _blank code).

my code for this page is:

<?php

include_once('include/connection.php');
include_once('include/article.php');

$article = new article();


if(isset($_GET['id'])) {
   $id = $_GET['id'];
$data = $article->fetch_data($id);

if ($param){
    echo '<div>' . $data["bla"] . '</div>';
}

$articles = $article->fetch_all();

?>

<html>

<head>
<title>xclo mobi</title>
<link rel="stylesheet" href="other.css" />
</head>

<body>
<?php include_once('header.php'); ?>


<div class="container">
<a href="index.php" id="logo">Category = ???</a>


    <?php foreach ($articles as $article) { 
    if ($article['promo_cat'] === $_GET['id']) { ?>


<div class="border">
<a href="single.php?id=<?php echo $article['promo_title']; ?>" style="text-decoration: none">
<img src="<?php echo $article['promo_image']; ?>" border="0" class="img" align="left"><br />


<a href="<?php echo $data['promo_link']; ?>" target="_blank"><img alt="" title="" src="GO.png" height="50" width="50" align="right" /></a>
<br /><br /><br /><br />
    <?PHP echo '<div class="title">' . $article['promo_title'] . '</div>'; ?>
<br />

<font class="content"><em><center><?php echo $article['promo_content']; ?></center></em></font>

</div><br/><br />

          </a>

 <?php } } } ?>

</div>
<?php include_once('footer.php'); ?>
</body>

</html>

however: if you click on one of the logos and be diverted to the single.php page and click the same go button on that page it works.

my code for the working single.php page is this.

<?php

include_once('include/connection.php');
include_once('include/article.php');

$article = new article;

if(isset($_GET['id'])) {
   $id = $_GET['id'];
$data = $article->fetch_data($id);

?>

<html>

<head>
<title>Xclo Mobi</title>
<link rel="stylesheet" href="other.css" />
</head>

<body>
<?php include_once('header.php'); ?>


<div class="container">
<a href="index.php" id="logo">Title Page</a><br /><br />

<div align="center">
<img src="<?php echo $data['promo_image']; ?>" class="img"><br />

<font class="title"><?php echo $data['promo_title']; ?></font>


<p>
<center><?php echo $data['promo_content']; ?></center>


<a href="<?php echo $data['promo_link']; ?>" target="_blank"><img alt="" title="" src="GO.png" height="50" width="50" align="right" /></a>

<br /><br />
<b><u>More From This Brand</b></u>

<br /><br />


<a href="index.php">&larr; Home</a>

</div>
<?php include_once('footer.php'); ?>
</body>

</html>



<?php
} else { 
 header('location: index.php');
 exit();
}

?>

Please can someone tell me why the list.php page does not work correctly? thank you.

kevstarlive


Solution

  • In the non-working example, this value contains no data:

    $data['promo_link']
    

    So the href attribute in the link tag has no URL, and the browser defaults to the current page. How you're fetching that data is slightly different between the two pages. In both cases you use an id value passed from the query string:

    $id = $_GET['id'];
    $data = $article->fetch_data($id);
    

    However, the two pages in question have different values on the query string. The link you posted in the question is:

    http://www.xclo.mobi/xclo2/list.php?id=FREE
    

    Whereas the link from that page to the working example is:

    http://www.xclo.mobi/xclo2/single.php?id=Free%20Sim%20Card
    

    The two id values are very different. The second one is finding a record with data in the promo_link field, the first one is not.

    Upon closer inspection, it looks like this may just be a typo in the non-working page. Notice how you're also fetching a list of records in that page:

    $articles = $article->fetch_all();
    

    And you're using that in the loop for the display elements on the page:

    <img src="<?php echo $article['promo_image']; ?>
    

    But when you get to the link, you use the $data object instead:

    href="<?php echo $data['promo_link']; ?>"
    

    Maybe you meant that to be this?:

    href="<?php echo $article['promo_link']; ?>"
    

    It looks like you shouldn't even need the $data object on that first page, since your intent is to loop through a list of records instead of fetching a single record. So you can probably remove the $data references entirely and not bother fetching it from the database on that page.

    I also suggest changing your variable names to not conflict with each other. When you declare the iterating variable in the loop:

    foreach ($articles as $article)
    

    You're colliding with an existing object of the same name:

    $article = new article();
    $data = $article->fetch_data($id);
    

    I don't know off-hand how PHP handles name collisions in variable scope, but if for no other reason than to just avoid confusion you should name them something different. Maybe something like this:

    foreach ($articles as $articleRecord)