Topic: Looping through array to build HTML

I have a form page that contains a couple of user inputs. The page loads data from the database and uses javascript to perform calculations using the user input and the values from the database. To get it to work, I have to write <span> tags with IDs that correspond to the values pulled from the database. I have it working, but want to improve the code to make it a bit more elegant. Here's what I have now:

<?php
/*** include the xajax bootstrap ***/  
 require_once("xajax_core/xajax.inc.php");
/*** a new xajax object ***/  
$xajax = new xajax();
/*** set the debug flag ***/  
//$xajax->setFlag('debug', true); 
/*** register a PHP function with xajax ***/ 
$xajax->registerFunction("myFunction");

function myFunction($row)
{
    // do some stuff based on $row like query data from a database and
    // put it into a variable like $newContent
    $user_name = "username";
    $password = "password";
    $database = "database";
    $server = "server";
    $db_handle = mysql_connect($server, $user_name, $password);
    $db_found = mysql_select_db($database, $db_handle);

    if ($db_found) {

    $result = mysql_query("SELECT * FROM database WHERE somevar='5'")
    or die(mysql_error());

    $row = mysql_fetch_assoc($result); //get values from db and pass them to variables representing HTML ids
    $var0 = $row['var0'];
    $var1 = $row['var1'];
    ...
    $varN = $row['varN'];
    
    mysql_close($db_handle);
    }
    // Instantiate the xajaxResponse object
   $objResponse = new xajaxResponse();
    
    // assign the innerHTML attribute of the element with id="SomeID" to whatever the new content is
    $objResponse->Assign("var0","innerHTML", $var0);
    $objResponse->Assign("var1","innerHTML", $var1);
    ...
    $objResponse->Assign("varN","innerHTML", $varN);

    
    //return the  xajaxResponse object
   return $objResponse;
}


$xajax->processRequest();
$xajax->printJavascript();
?>

I'd like to use something like this to write the spans:

       foreach ($row as $c) {
        while (list($k,$v) = each ($c)) {
                echo "<span id=\"$v\"></span>";
                }
   }

I'm new to xajax and would appreciate any help to get me going in the right direction.

Re: Looping through array to build HTML

hi
if i got your question right, just have a span or div with an ID. in this span, create your spand via xajax and in your foreach function use the append() function of xajax.
http://www.xajax-project.org/en/docs-tu … se/append/

like this:
$objResponse->append('my_ueber_span_1', 'innerHTML', '<span id=\"$v\"></span>');
       
return $objResponse;

3 (edited by Fishboy 2011-07-06 11:00:43 PM)

Re: Looping through array to build HTML

Thanks Mazi. To clarify, the project I'm working on will be replicated for many clients, so I'm looking for ways to dynamically generate code to accomplish several things:

1. I'd like to be able to loop through the array to equate variables withe the key/values in the array without listing them all like I have been in $var0 = $row['var0']; etc.

2. I'd also like to be able to assign the variable values in the response object to the IDs of form or span elements in my code without listing them all like I have been in $objResponse->Assign("var0","innerHTML", $var0); etc.

3. I've been hand coding the spans with the IDs assigned in the response object. Your suggestion would work to append the spans to a div, since I need a div wrapped around the spans so I can set the div to display:none. I've been experimenting with the foreach loop in simple version of code, but can't get it to work in my actual code. I'm not sure where it should go or if I can accomplish the goals above using this kind of solution.

Any ideas?

Re: Looping through array to build HTML

After more experimentation, I came up with a way to do what I wanted:

    $result = mysql_query("SELECT * FROM database WHERE something='5'")
    or die(mysql_error());
    $row = mysql_fetch_assoc($result);
   
    mysql_close($db_handle);
    }
    // Instantiate the xajaxResponse object
   $objResponse = new xajaxResponse();
    foreach ($row as $key => $value) {
        if ($value != 0){
    //echo '<span id="'.$key.'">'.$value.'</span> | ';
    $objResponse->append('spans', 'innerHTML', '<span id="'.$key.'">'.$value.'</span> | ');
        }
    }

   return $objResponse;

This loops through the array and writes the response objects into this div:

<div id="spans" style="display:none"></div>

I only wanted to return the values that were not equal to zero, that's why the "if" statement is there.

Re: Looping through array to build HTML

Glad it works and that i could help.