Embeding PHP code in javascript!!

Discussion in 'PHP' started by ausgezeichnete, Jan 8, 2008.

  1. #1
    i want to replace this array in this javascript code to data from the database

    
    Book_Image_Sources=new Array(
    "images/1199696066.jpg","",
    "images/1199710328.jpg","",
    "images/1199772425.jpg","",
    "images/1199772431.jpg","",
    "images/1199772448.jpg","",
    "images/1199772904.jpg","",
    "images/1199772920.jpg","",
    "images/1199773091.jpg","",
    "images/1199797162.jpg","",
    "images/1199797196.jpg","",
    "images/1199797235.jpg","",
    "images/1199797258.jpg","",
    "images/3.jpg","" // NOTE No comma after last line
    );
    
    PHP:
    to replace it to
    
    $query=mysqli_query($conn,"select client_images_fotos from client_images");	
    $records=mysqli_fetch_array($query);
    
    PHP:
    i tried to do it like that but it didnt work
    
      <?php do { ?>
    Book_Image_Sources=  <?php echo $row_Recordset1['client_images_fotos']; ?>
    
        <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    
    PHP:
     
    ausgezeichnete, Jan 8, 2008 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    You need to do it like,

    
    $query=mysqli_query($conn,"select client_images_fotos from client_images"); 
    $total=mysqli_num_rows($query);
    $i=0;
    while($i<$total)
    {
    echo "Book_Image_Sources= ".mysql_result($query, $i, 'client_images_fotos');
    $i++;
    }
    
    PHP:

    Havent tested should work
     
    HuggyStudios, Jan 8, 2008 IP
  3. ausgezeichnete

    ausgezeichnete Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    noooo it didnt work
    
    
    <?php  
    $conn=mysql_connect("localhost","epicalex","10203040");
    mysql_select_db("mystiq");
    
    $query=mysql_query("select client_images_fotos from client_images");
    if(!$query){echo mysql_error();}else{echo "yes";}
    $total=mysql_num_rows($query);
    echo $total;
    $i=0;
    
    
    ?>
    
    <!--CMBookFlipHS-->
    <script type="text/javascript">
    
    /***********************************************
    * Book Flip slideshow script- © Ger Versluis 2003
    * Permission granted to DynamicDrive.com to feature script
    * This notice must stay intact for use
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    // 7 variables to control behavior
    var Book_Image_Width=300;
    var Book_Image_Height=300;
    var Book_Border=true;
    var Book_Border_Color="black";
    var Book_Speed=15;
    var Book_NextPage_Delay=2*1000; //1 second=1000
    var Book_Vertical_Turn=0;
    
    // array to specify images and optional links. At least 4
    // If Link is not needed keep it ""
    <?php
    while($i<$total)
    {
    echo "Book_Image_Sources= ".mysql_result($query, $i, 'client_images_fotos');
    $i++;
    
    }
    ?>
    
    PHP:
     
    ausgezeichnete, Jan 8, 2008 IP
  4. ausgezeichnete

    ausgezeichnete Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    it ecgo the results when i put it in the top under the select query
    and gave me
    but when i put it inside the javascript it didnt echo anything
     
    ausgezeichnete, Jan 8, 2008 IP
  5. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i'm not sure if i understood what you exactly want to do but this might help:

    <?php 
    $conn=mysql_connect("localhost","epicalex","10203040");
    mysql_select_db("mystiq");
    
    $query=mysql_query("select client_images_fotos from client_images");
    if(!$query){echo mysql_error();}else{echo "yes";}
    $total=mysql_num_rows($query);
    echo $total;
    $i=0;
    
    
    ?>
    
    <!--CMBookFlipHS-->
    <script type="text/javascript">
    
    /***********************************************
    * Book Flip slideshow script- © Ger Versluis 2003
    * Permission granted to DynamicDrive.com to feature script
    * This notice must stay intact for use
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    // 7 variables to control behavior
    var Book_Image_Width=300;
    var Book_Image_Height=300;
    var Book_Border=true;
    var Book_Border_Color="black";
    var Book_Speed=15;
    var Book_NextPage_Delay=2*1000; //1 second=1000
    var Book_Vertical_Turn=0;
    
    // array to specify images and optional links. At least 4
    // If Link is not needed keep it ""
    Book_Image_Sources=new Array(
    <?php
    while($i<$total)
    {
    echo '"'.mysql_result($query, $i, 'client_images_fotos').'",""';
    if($i!=$total){
    	echo ',';
    }
    $i++;
    
    }
    ?>
    );
    PHP:
     
    Sepehr, Jan 8, 2008 IP
  6. ausgezeichnete

    ausgezeichnete Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thnxxxx but it gave me the same results nothing appeared even the image place
    am making a book flip from dreamwaver
    but it generates it static and i want it dynamically from the db
     
    ausgezeichnete, Jan 8, 2008 IP
  7. ausgezeichnete

    ausgezeichnete Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thnxxx i found the answer
    
    if ($result = $mysqli->query($query)) {
    
        $tmp = array();
    
        while ($row=$result->fetch_assoc()) {
    
           $tmp[] = $row['client_images_fotos'];
    
        }
    } 
    
    
    ?>
    
    <!--CMBookFlipHS-->
    <script type="text/javascript">
    
    /***********************************************
    * Book Flip slideshow script- © Ger Versluis 2003
    * Permission granted to DynamicDrive.com to feature script
    * This notice must stay intact for use
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    // 7 variables to control behavior
    var Book_Image_Width=300;
    var Book_Image_Height=300;
    var Book_Border=true;
    var Book_Border_Color="black";
    var Book_Speed=15;
    var Book_NextPage_Delay=2*1000; //1 second=1000
    var Book_Vertical_Turn=0;
    
    // array to specify images and optional links. At least 4
    // If Link is not needed keep it ""
    <?php
     $js = sprintf("Book_Image_Sources=new Array('%s','');",implode("','','",$tmp));
    
        echo $js;
    ?>
    
    
    
    PHP:
     
    ausgezeichnete, Jan 8, 2008 IP
  8. ausgezeichnete

    ausgezeichnete Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    but one thing please
    the first image appears empty cause the loop begins from 0 to the array length
    how can i make it begin with the first value
     
    ausgezeichnete, Jan 8, 2008 IP