Jump to content

Can't replace value with string.


Love2c0de
Go to solution Solved by dalecosp,

Recommended Posts

Good afternoon all,

 

I'm trying to setup a website and having a problem loading in dynamic content.

 

I have a main index.php file loading in template files - some which need to be dynamic.

 

I have a value within my template Home file called <__CONTENT__> and I'm trying to replace that with some images which have been retrieved from a database. It does replace the value with my output, but my homepage still displays the <__CONTENT__> value.

 

Here is my main index.php file:

<?php
session_start();
require("core/setup.php");
 
 
 
?>
<!DOCTYPE>
<html lang="en">
<head>
<title>Gardenable | <?php print($title); ?></title>
<link rel="stylesheet" href="css/font.css" />
<link rel="stylesheet" href="css/main.css" />
<link rel="stylesheet" href="css/respond" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
 
<body>
 
<div id="container">
<div id="header">
<img src="images/logo.png" alt="Gardenable - Making gardening accessible to everyone" id="logo" />
 
<div id="top_mid">
<p>Already registered? <a href="#">Login</a></p>
<p>Need an account? <a href="#">Register</a></p>
</div>
 
<ul id="navigation">
<li><a href="?page=Home">home</a></li>
<li><a href="?page=Products">products</a></li>
<li><a href="?page=Services">services</a></li>
<li><a href="?page=About">about</a></li>
<li><a href="?page=Contact">get in touch</a></li>
</ul>
</div>
 
<?php if(isset($left_column)){ print($left_column); } ?>
 
<div id="main_content"> 
<?php if(isset($content)){ include($filepath); } ?> 
 
</div>
 
<hr class="hidden_rule" />
</div>
 
</body>
</html>

This loads setup.php first:

function getDynamic($path,$page)
{
$path = substr_replace($path, "", strrpos($path, "/"));
$path .= "/queries/";
 
switch($page)
{
case 'Home':
$planters = require($path."getproducts.php");
return $planters;
break;
 
case 'Products':
 
break;
 
}
 
}
 
if(!isset($_GET['page']))
{
header("Location: ?page=Home");
die();
}
 
$_GET['page'] = "Home";
 
$page = $_GET['page'];
 
$path = $_SERVER['DOCUMENT_ROOT'] . "clients/Garden/core/content";
 
$pathinfo = pathinfo($path, PATHINFO_FILENAME);
 
$filepath = $path . "/" . $_GET['page'] . ".htm";
 
if(file_exists($filepath))
{
$planters = getDynamic($path,$page);
$content = file_get_contents($filepath);
var_dump($planters);
var_dump($content);
$content = str_replace("<__CONTENT__>", $planters, $content);
}
else
{
header("Location: ?page=Home");
die();
}

Here is my getproducts.php file:

<?php
 
// $dir = substr_replace($_SERVER['SCRIPT_FILENAME'], "", strrpos($_SERVER['SCRIPT_FILENAME'], "/") + 1);
 
$conn = new mysqli("localhost","root","","gardenable") or die("Error creating connection.");
 
$stmt = $conn->prepare("SELECT * FROM planter_images WHERE productID=? LIMIT 10");
 
//rand = 1,1 because not all images are in db yet, the only ID present is 1. <----modify once table complete.
$stmt->bind_param("i",rand(1,1));
$stmt->execute();
$stmt->bind_result($i_id, $p_id, $i_name, $p_price, $p_dims, $p_ref, $p_desc);
$stmt->store_result();
 
$row = $stmt->num_rows;
 
if($row)
{
$headings = array("Raised Planters","Rounded Planters");
$hcount = 0;
 
//image counter.
$x = 1;
 
$planters = "<div id='home_product_main'>";
$planters .= "<h3>".$headings[$hcount]."</h3>";
 
while($stmt->fetch())
{
$num = substr($i_name, strpos($i_name, "{$x}"), 1);
 
$planters .= "<div class='product_holder'>";
$planters .= "<a href='#'>";
$planters .= "<img src='core/content/planter_imgs/{$i_name}' class='home_prods' alt='Image not loading? Contact the administrator at admin@gardenable.co.uk' />";
$planters .= "</a>";
$planters .= "</div>";
 
if($num === "5")
{ 
$planters .= "<hr class='separator' />";
$planters .= "<h3>".$headings[++$hcount]."</h3>";
 
//reset image counter.
$x = 1;
}
else
{
$x++;
}
}
 
$planters .= "</div>";
}
 
return $planters;
 
mysqli_close($conn);
?>

Here is my Home.htm file:

<p>Gardenable is a garden product and services company based in Clacton on Sea in Essex. We specialize in designing and creating bespoke garden products and also provide local garden services such as decking, jet washing and hedge trimming.</p>
 
<h3>Featured Products</h3>
 
<__CONTENT__>
 
 
 
<p>Gardenable is a garden product and services company based in Clacton on Sea in Essex. We specialize in designing and creating bespoke garden products and also provide local garden services such as decking, jet washing and hedge trimming.</p>

I've attached an image.

 

As you can see from the var_dump() the replacement has been performed and I later echo the $content variable out AFTER it has been changed. 

post-128658-0-18254800-1383662060_thumb.png

Any ideas?

 

Thank you for your time.

 

Kind regards,

 

L2c.

 

Edited by Love2c0de
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.