www.webdeveloper.com
Results 1 to 4 of 4

Thread: Code compilation issues

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    2

    Unhappy Code compilation issues

    Hello scripters...

    This has probably been addressed already, but I want to be able to change elements like headers, paragraph information within one part of the site and do this using an array of images. For instance, if you were to click on image1, the div that may hold the information, name of image, image description, and price, for example, will change.

    Now, I know how to do this using a basic function technique, but I end up having to separate the code for each image, and I would like to store all the information lets say, on a database, and call the information as the user clicks. I know that there is a looping technique to do this, but I can not get it to work properly.

    I use PHP to call the information from the database, and then the javascript code eludes me to do this. I hope that this makes sense, and if nor infor is needed, please reply and I will try to answer.

  2. #2
    Join Date
    Jan 2005
    Posts
    372
    Def. need more info to understand what the problem is.

  3. #3
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    So you've got a navigation at some point on your site with images that, when clicked, will change parts of the page?

    You said you want to call the information as the user requests it from the clicks. I would not recommend that method as you will ultimately end up using more bandwidth and server power. You're better off calling all your data in one request on page load and storing it in a multi-dimensional PHP array, which can be echoed in to JavaScript arrays for client-side use.

    Because you've given no code and not much information, I can't give exact code, but it would be something along the lines of:

    PHP Code:
    // get all the data from the necessary table
    $get_data mysql_query("SELECT * FROM some_table");

    for(
    $i 0$i mysql_num_rows($get_data); $i++)
    {
        
    // store the table rows in to a PHP 3-dimensional associative array.
        
    $results[$i] = mysql_fetch_assoc($get_data);

    Let's say you've got 3 columns in your database: name, age, gender. These are now stored in PHP as: $results[$i]['name'], $results[$i]['age'] and $results[$i]['gender'].

    You can now echo these results into JavaScript arrays for use on the client-side. You will create a JavaScript 3-dimensional array like so:

    Code:
    var details= Array(name = Array(), 
                        age = Array(), 
                     gender = Array());
    Now in PHP you can echo the results in to each of these arrays like so:

    PHP Code:
    <script language="javascript">
    <?php for($i 0$i count($results); $i++)   echo "details[0][".$i."] = '" $result[$i]["name"] . "',"?>
    <?php 
    for($i 0$i count($results); $i++)   echo "details[1][".$i."] = '" $results[$i]["age"] . "',"?>
    <?php 
    for($i 0$i count($results); $i++)   echo "details[2][".$i."] = '" $results[$i]["gender"] . "',"?>
    </script>
    At this point you've basically converted a 3D PHP array to a 3D JavaScript array and can now use it on the client-side.

    Because you've not given much information I can't tell you the next step. However, since you said you want to change the content of a <div> with a click of the mouse, you can use the document.getElementById() object in JavaScript to achieve this result.

    For example, a button with an onClick event which calls a function "change();". Your "change();" function would look something like:

    Code:
    function change()
    {
        document.getElementById("your_div_id").innerHTML = "something";
    }
    You can also create a variable and give it a string value containing HTML, and set the .innerHTML value to the value of the variable.

    Please provide more information so we can help more.
    Last edited by George88; 02-23-2012 at 05:43 PM.

  4. #4
    Join Date
    Feb 2012
    Posts
    2

    Thumbs up Thank you

    I do appreciate the input. I do apologize that I didnt leave any real code... I know how it looks in my head, but I am not so advanced in javascript to even make it plain, but I do understand what you have written as an example. I think that I can build from it, and if I get stuck, I will post the issue in code then.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles