Page 1 of 1

Passing and returning content

Posted: Sat Feb 27, 2010 3:26 am
by robustbrad
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

Re: Passing and returning content

Posted: Sat Feb 27, 2010 9:26 am
by social_experiment
You would retrieve the value of id from the $_GET['id'] variable

Code: Select all

<?php $id = $_GET['id']; ?>
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.

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
 }
?>
With '$pointer['Some Information'] you would change to the fields you want to display from the database.

Hope this helps

Re: Passing and returning content

Posted: Sat Feb 27, 2010 12:04 pm
by robustbrad
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

Re: Passing and returning content

Posted: Sat Feb 27, 2010 2:02 pm
by social_experiment
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.

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']); ?>      
?>
 
Also, take a look at SQLite. From PHP Manual :
This 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.
This is the url for the SQLite website
http://sqlite.org/

Re: Passing and returning content

Posted: Sat Feb 27, 2010 2:09 pm
by mikosiko
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
define how much and what kind of information is "some information"

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

Re: Passing and returning content

Posted: Sat Feb 27, 2010 2:17 pm
by robustbrad
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?

Re: Passing and returning content

Posted: Sat Feb 27, 2010 2:18 pm
by robustbrad
mikosiko wrote:
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
define how much and what kind of information is "some information"

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.