Hey guys,
First post here. I'm pretty new to php but not new to programming. This probably will come across as a very basic question and hopefully solution, but here goes.
So I have various different images, lets say 3. When clicked on I want to dynamically load a section of page on my website. For example if a visitor clicks on image 3 I would call images.php?id=3.
So here lies my question. What would the code look like in the PHP file itself. I'd like the code below where "some information" is to change dynamically to what image is passed to images.php file (for example if I passed images.php?id=1, images.php?id=2, images.php?id=3) to return different information.
<h1 class="title">Some information</h1>
<div id="scroll">Some information</div>
<div class="footer">Some Information</div>
<div id="left">Some Information</div>
Hope that helps. Thank you in advance for helping.
Cheers,
Brad
Passing and returning content
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Passing and returning content
You would retrieve the value of id from the $_GET['id'] variable
Depending on where your information is stored (database or flatfile) you would use the value from '$id' to retrieve the information. Here is an example for using the value of '$id' when retrieving data from a SQL database.
With '$pointer['Some Information'] you would change to the fields you want to display from the database.
Hope this helps
Code: Select all
<?php $id = $_GET['id']; ?>Code: Select all
<?php
$id = $_GET['id'];
if ($id != '' && is_numeric($id) ) {
//connect to the database
$q = mysql_query("SELECT fieldsYouWant FROM yourTable WHERE fieldToMatchWith = '".mysql_real_escape_string($id)."' LIMIT 1");
while ($pointer = mysql_fetch_array($q)) {
echo "<h1 class=\"title\">$pointer['Some information']</h1>";
echo "<div id=\"scroll\">$pointer['Some information']</div>";
echo "<div class=\"footer\">$pointer['Some Information']</div>";
echo "<div id=\"left\">$pointer['Some Information']</div>";
}
//close connection to database if
//not needed anymore
}
?>Hope this helps
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
robustbrad
- Forum Newbie
- Posts: 4
- Joined: Sat Feb 27, 2010 3:15 am
Re: Passing and returning content
Thank you so much for the reply. I'm not really sure if I'm ready to query databases yet however. Is there a more simpler approach to this where all I have in the .php file is a bunch of if/then statements and all the content is stored directly in the php file?
Thanks in advance,
Brad
Thanks in advance,
Brad
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Passing and returning content
There are numerous problems with using files are a 'storing' system.
If the filenames are know they can be directly be accessed via URL.
As your information increases, the amount of pages will also increase.
However, if this is the route you want to go, here is a function that could be of use.
Also, take a look at SQLite. From PHP Manual :
http://sqlite.org/
If the filenames are know they can be directly be accessed via URL.
As your information increases, the amount of pages will also increase.
However, if this is the route you want to go, here is a function that could be of use.
Code: Select all
<?php
function displayPage($value) {
//check that the value is numeric and
//present
if ($value != '' && intval($value)) {
$extension = '.php';
$new_Value = $value.$extension;
//once the file name has been compiled
//check if a file with this name exists
if (file_exists($new_Value)) {
@require $new_Value;
}
//inform the user that the file
//does not exist
else {
echo 'File does not exist';
}
}
else {
//if the value is NOT numeric or is
//indicate this to the user.
echo 'File not found';
}
}
//call the function
<?php displayPage($_GET['id']); ?>
?>
This is the url for the SQLite websiteThis is an extension for the SQLite Embeddable SQL Database Engine. SQLite is a C library that implements an embeddable SQL database engine. Programs that link with the SQLite library can have
SQL database access without running a separate RDBMS process.
SQLite is not a client library used to connect to a big database server. SQLite is the server. The SQLite library reads and writes directly to and from the database files on disk.
http://sqlite.org/
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: Passing and returning content
define how much and what kind of information is "some information"robustbrad wrote:Hey guys,
.....
So I have various different images, lets say 3. When clicked on I want to dynamically load a section of page on my website. For example if a visitor clicks on image 3 I would call images.php?id=3.
So here lies my question. What would the code look like in the PHP file itself. I'd like the code below where "some information" is to change dynamically to what image is passed to images.php file (for example if I passed images.php?id=1, images.php?id=2, images.php?id=3) to return different information.
<h1 class="title">Some information</h1>
<div id="scroll">Some information</div>
<div class="footer">Some Information</div>
<div id="left">Some Information</div>
Hope that helps. Thank you in advance for helping.
Cheers,
Brad
even when your description is not totally clear seems to me that your are looking for some kind of Ajax implementation... look for some basic examples here:
http://www.w3schools.com/ajax/default.asp
-
robustbrad
- Forum Newbie
- Posts: 4
- Joined: Sat Feb 27, 2010 3:15 am
Re: Passing and returning content
I guess my explanation is a little foggy. After doing some research i'll throw the pseudo code of what I'm thinking.
User clicks on an image (1) on my main page. It triggers image.php?id=1
----------------------------------------------------
image.php file would include:
Getid(id)
Switch(id)
Case(1);
Return some HTML
Case(2);
Return some HTML
Case (3);
Return some HTML
-------------------------------------------
Would that be a viable solution? If so, what would the actual code look like?
User clicks on an image (1) on my main page. It triggers image.php?id=1
----------------------------------------------------
image.php file would include:
Getid(id)
Switch(id)
Case(1);
Return some HTML
Case(2);
Return some HTML
Case (3);
Return some HTML
-------------------------------------------
Would that be a viable solution? If so, what would the actual code look like?
-
robustbrad
- Forum Newbie
- Posts: 4
- Joined: Sat Feb 27, 2010 3:15 am
Re: Passing and returning content
mikosiko wrote:define how much and what kind of information is "some information"robustbrad wrote:Hey guys,
.....
So I have various different images, lets say 3. When clicked on I want to dynamically load a section of page on my website. For example if a visitor clicks on image 3 I would call images.php?id=3.
So here lies my question. What would the code look like in the PHP file itself. I'd like the code below where "some information" is to change dynamically to what image is passed to images.php file (for example if I passed images.php?id=1, images.php?id=2, images.php?id=3) to return different information.
<h1 class="title">Some information</h1>
<div id="scroll">Some information</div>
<div class="footer">Some Information</div>
<div id="left">Some Information</div>
Hope that helps. Thank you in advance for helping.
Cheers,
Brad
even when your description is not totally clear seems to me that your are looking for some kind of Ajax implementation... look for some basic examples here:
http://www.w3schools.com/ajax/default.asp
"some information" would just be some basic HTML arguments as I mentioned in my first post.