Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Location
    New Jersey
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP in Javascript (or the otherway)

    I am looking for an easy way to add image links to my web site. I'd like to add the image name, file name and type to a database then have PHP output them on the page. BUT I would like the output to be entered in a Javascript to utilize what the Javascript has to offer.

    The end result of the Javascript is:

    <a href="test.gif" onClick="return enlarge('test.gif',event,'center', 594, 350)"><strong>Test Image</strong></a>

    I need the PHP output to go where both test.gif(s) are in the above code and, of course, to be repeated as many times as there are database entries for that type of image.

    The end result would be:

    <a href="test.gif" onClick="return enlarge('test.gif',event,'center', 594, 350)"><strong>Test Image</strong></a>

    <a href="second.gif" onClick="return enlarge('second.gif',event,'center', 594, 350)"><strong>Test Image</strong></a>

    <a href="another.gif" onClick="return enlarge('another.gif',event,'center', 594, 350)"><strong>Test Image</strong></a>

    etc.

    Thanks,

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Just use PHP to echo out anything you want used in the script:

    PHP Code:



    <a href="test.gif" onClick="return enlarge('<?php echo $imagevariable?>',event,'center', 594, 350)"><strong>Test Image</strong></a>
    If you need to output a bunch of links to images then set up a loop and have PHP output either the entire link or just echo the parts of the link that correspond to the data retrieved from your database query.
    Last edited by Spookster; 05-12-2004 at 05:41 PM.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New to the CF scene
    Join Date
    May 2004
    Location
    New Jersey
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks,

    So far I have the following coding:

    PHP Code:
    <?php

    $dbh
    =mysql_connect ("localhost""user""password") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("database",$dbh);
    $sql "SELECT * From areas where type1 = 'divident'";
    $result mysql_query($sql$dbh) or die(mysql_error());
    while (
    $newArray mysql_fetch_array($result)){
    $type1 $newArray['type1'];
    $type2 $newArray['type2'];
    $image $newArray['image'];
    $file $newArray['file'];

    }
    ?>
                  
    <p><a href="<?php echo $file?>" onClick="return enlarge('<?php echo $file?>',event,'center', 450, 250)"><strong>
    <?php echo $image; ?/strong></a></p>
    -----

    This will output exactly what I need. My problem now is how to make the loop so all the entries are listed. What would you suggest?

    Thanks, (as you can see, I'm very new to this. The last time I programed, computers were the size of a room and you had to use punch cards for input)
    Last edited by Spookster; 05-13-2004 at 04:05 AM. Reason: Added [PHP] [/PHP] tags. Please use them in the future when posting code.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    What you need to do is loop through the recordset returned from your query.

    PHP Code:

    <?php 

    $dbh
    =mysql_connect ("localhost""user""password") or die ('I cannot connect to the database because: ' mysql_error()); 
    mysql_select_db ("database",$dbh); 
    $sql "SELECT * From areas where type1 = 'divident'"
    $result mysql_query($sql$dbh) or die(mysql_error()); 

    while(
    $row mysql_fetch_array($result)){
    ?> 
    <p><a href="<?php echo $row['fieldname']; ?>" onClick="return enlarge('<?php echo $row['fieldname']; ?>',event,'center', 450, 250)"><strong> 
    <?php echo $image; ?/strong></a></p

    <?
    php
    }//end while loop 
    ?>
    Just change "fieldname" to whatever the column name is in your database table that you want to use.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New to the CF scene
    Join Date
    May 2004
    Location
    New Jersey
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I've been able to make everything work fine. Now I have to disect what you've done so I can understand the mechanics.

    By doing it this way, I will save a lot of time. I am currently redesigning my web site that is over 4000 pages. Here is a finished page:

    http://www.oldnewark.com/areas/divident.php

    Thanks again,


  •  

    Posting Permissions

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