SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    database into array

    Hi,

    I'm making a list on a web page of my different books (exemple).

    Code:
    <a href="#" id="permissions1">my favorite book </a>
    <a href="#" id="permissions2"> best selling book </a>
    <a href="#" id="permissions3">most expensive book </a>
    When the user clicks on the link, I want to do a dropdown sheet with the info of the book (the info is in a mysql datebase), name , author, nb pages etc)

    I have generated onclick fonctions automatically, that passes a parameter (ie 1,2,3) but don't know how link it to the info in my database.
    There are only 25 books, so I could just create an array and get it that way.
    But the day that I modify info in my database, I'd need to correct my code and I would like to avoid that.

    TIA
    Patricia

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have three options as I see it:

    1. When the user clicks a link, open a new page with the results.
    2. Load all your book data into a javascript array and manipulate it client side.
    3. Use AJAX to send/return data from the server and populate your lists via javascript.

    If your site needs to be accessible to all browsers with/without javascript, your only option is #1.
    If your database is very small and will stay that way, #2 is the simplest albeit limiting approach.
    If your database might grow a lot, and you don't like option 1, you can try option 3 but coding AJAX requires skill and you will still need server-side script support.

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >>2. Load all your book data into a javascript array and manipulate it client side.
    How can I do this ?

    I have found some examples to get the data into a javascript array

    Code:
    $result=mysql_query('SOME SELECT');
    $jsArray="var item = new Array();";
    for($I=0;$I<mysql_num_rows($result);$I++)
    {
    list($item,$value,$anothervalue)=mysql_fetch_row($result);
    $jsArray.="item[".$I."]=new Array('".$item."','".$value."','".$anothervalue."');";
    }
    I haven't tried it, but I do understand the principle.
    However I just don't know how to tell the onclick event about the array ?
    Unless $jsarray is a reserved word, but can't find any reference to it.

    I'm generating the onclick using the below code. I do know if mylist=1, it is the first row in the array, if 2, 2nd etc. The array will be generated in my php code, but how can I reference it from createDialog ?

    Code:
    function createDialog(mylist)
    {
    // need to add a param function createDialog(mylist) ,
      var body = document.getElementsByTagName("body")[0];
      var pageDimensions = getPageDimensions();
      var viewportSize = getViewportSize();
    
    // need to test mylist, then will fill in the proper info below, for the moment it is just printin name, addresse
    var printname = "Clinique de l'Auzon  "  ;
    if (mylist > 1)
      {
        var printname = "Clinique mazuel  "  ;
      }

    Thanks for the help
    Patricia

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In you example the PHP variable $jsarray is a big string full of definitions for a javascript array called item, so once you've finished populating it you just need to write it out in a <script> section of your html:
    Code:
    <script>
    var myThing = 1;
    <?=$jsArray?>
    var myOtherThing = 2;
    </script>
    Once you have it in javascript you are free to use the item array:
    Code:
    function createDialog(mylist)
    {
    // need to add a param function createDialog(mylist) ,
      var body = document.getElementsByTagName("body")[0];
      var pageDimensions = getPageDimensions();
      var viewportSize = getViewportSize();
    
    // need to test mylist, then will fill in the proper info below, for the moment it is just printin name, addresse
    var printname = "Clinique de l'Auzon  "  ;
    if (mylist > 1)
      {
        printname = item[mylist][0];
      }

  5. #5
    SitePoint Addict
    Join Date
    May 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    Thanks for your help, I haven't gotten it all working but I am avancing quite nicely.

    Ok, I seem to be generating the array properly, but my fonction doesn't appear to like my affecation. If I use only the first var, all works fine, if I rem out the first and use the second, my function doesn't work.

    The function createDialog is called when I am building my page, it generats an onclick. If I put only var printname = noms[mylist], then when the person clicks on the link, the generated code gets executed properly. If I use var printname = item[mylist][0] ;, when the person clicks on on the link nothing happens.

    I don't know if I have a syntaxe error, or a problem with the javascript finding item.

    Here are the different lines of code.

    Code:
    function createDialog(mylist)
    .....
    //var printname = noms[mylist] ;
    var printname = item[mylist][0] ;
    ...
    this is the PHP code that is used to set up each line in the array
    PHP Code:
    $jsArray.="item[".$i."]=new Array('".$ligne[libelle]."','".$ligne[adr]."','".$ligne[cp]."');"
    this is the javascript code I use to generate the array, the follows the above code.
    Code:
    <script>
    	var myThing = 1;
    	<?=$jsArray?>
    	var myOtherThing = 2;
    </script>
    and this is what I see in my source where I have put my links, so it appears to be generated correctly .
    var item = new Array();item[0]=new Array('Ange Gardien','rue Léopold Bellan','77260');item[1]=new Array('Auzon','Les Plats','63670');item[2]=new Array('Belle Allée','24 route d'Orléans','45380');item[3]=new Array('Escrebieux','984 rue de Quiéry','59553');item[4]=new Array('Espérance','6 rue de la Borderie','35000');
    Last edited by patmazuel; Aug 22, 2006 at 22:14.


Bookmarks

Posting Permissions

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