0

been trying to save my data from my while loop to script variables but no success. Did an example if how I want to save my php data to script variables. This doesn't work for me. Anyone have any idea? Don't want to save all data manually. Very greatful for answers! Ask if you don't understand :) Right now it only saves the last variable in the row2.

$id = 0;
while($rows = mysql_fetch_array($data)){
$id = $id + 1;

$data = $rows['data'];

    $id2 = 0; 
    $sql = mysql_query("select * from table where id = '".$data."'");
    while($row2 = mysql_fetch_array($sql)){

    $id2 = $id2 + 1;
    $var = $row2['var']; //lets say this one is HT123
    }

$savethis = "var data" . $id . "_" . $id2 . " = '" . $var . "';"; 
}

echo "<script>";

   echo $savethis;

   //I want it to equal like "var data1_1 = 'HT123';
   //And then do this for each row, so:
   //var data1_2 = 'HW132';
   //var data1_3 = 'PQ542';
   //var data2_1 = 'SA210';
   //Etc

echo "</script>";
4
  • Just in case you didn't know already, please note that PHP's mysql_xxx() functions are officially deprecated. They will probably be removed entirely from PHP in a future release. It is recommended to switch to an alternative API. PHP provides either the mysqli or PDO libraries which can be used instead. Commented Nov 6, 2013 at 14:44
  • Yes, heard that before. Do I need any special program to use mysqli and PDO? Doing everything in notepad++ and saves it as .php @Spudley Commented Nov 6, 2013 at 14:49
  • No, nothing special: PDO and mysqli are just another set of functions and classes built into PHP, exactly the same as the mysql functions. The only difference is that the functions you're using now are obsolete. Commented Nov 6, 2013 at 14:54
  • They aren't meant to be used in conjunction with each other, you should pick one or the other. I recommend PDO since it supports not only MySQL but PostgreSQL and others, so it is more flexible. You can learn one way to write code and it can translate much easier. It is explained more in depth here Commented Nov 6, 2013 at 15:07

4 Answers 4

1

I'd really recommend using PDO as mysql_fetch_array() will be deprecated in the near future.

Having that in mind, if you could do it with PDO (here is where you can learn to set up a PDO connection), I think this might work (just working off the top of my head as I don't have your data to work with:

// code to set up a PDO connection
$sql = "SELECT * FROM first_table";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$first_results = $stmt->fetchAll(PDO::FETCH_OBJ);

$result_array = array();

foreach ($first_results AS $first) {

    $newSQL = "SELECT * FROM second_table WHERE id = '{$first->id}'";
    $newStmt = $pdo->prepare($newSQL);
    $newStmt->execute();
    $second_results = $newStmt->fetchAll(PDO::FETCH_OBJ);

    // append
    $result_array[$first->id][$second_results->id] = $second_results->var; 
}

return json_encode($result_array);

Again, that is just a rough idea and might have kinks in it. But that should give you nested arrays keyed by the ids. So it might look (hopefully) like this:

[0]
  [0] => "var", (this would be equivalent to data0_0 = var)
  [1] => "var2"
[1]
  [0] => "foo"

You can have them interact by using AJAX to send the request to your PHP file, then you can expect the JSON array as the response.

Sign up to request clarification or add additional context in comments.

Comments

0

you can use json_encode to do the dirt work for you!

echo json_encode($data);

this will encode whatever you have ti json format(no matter array or object or variable)

3 Comments

Not pretty sure how to do this :S @tinyByte
try it yourself then you'll find out, I'm doing this all the time for my ajax forms
OKay, will try to find ;) @tinyByte
0

You need to place $varin Apostroff. Without it you will have error in js code.

$savethis = "var data" . $id . "_" . $id2 . " = '" . $var . "';"; 

Comments

0

done this stuff before. just gonna give you a rough idea on how will this work.

add this before "while" then increment after while loop:

    $x=1;

set $var to array and count its value after while loop:

    $var[$x] = $row2['var'];

code will then look like this:

    $x=1;
    while($row2=mysqli_fetch_array($sql) )
    {
        $var[$x] = $row2['var'];
        $x++;
    }
    $array_length=count($var);

loop "script":

    $id=1;
    $id2=1;
    echo "<script>";
    for($i=1; $i<=$array_length; $i++)
    {
        $savethis = "var data" . $id++ . "_" . $id2++ . " = '" . $var[$i] . "';";
        echo $savethis;
    }
    echo "</script>";

AND THIS IS HOW YOUR COMPLETE CODE WILL LOOK LIKE:

    $x=1;
    while($row2=mysqli_fetch_array($sql) )
    {
        $var[$x] = $row2['var'];
        $x++;
    }
    $array_length=count($var);

    $id=1;
    $id2=1;
    echo "<script>";
    for($i=1; $i<=$array_length; $i++)
    {
        $savethis = "var data" . $id++ . "_" . $id2++ . " = '" . $var[$i] . "';";
        echo $savethis;
    }
    echo "</script>";

core solution revolves around array. hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.