Jump to content

Put array that is in a variable into mysql - more than 1 row per query


Recommended Posts

This code pulls numbers (type is varchar) from mysql. Then it finds every possible combination of the numbers. Then adds the numbers within each combination. So 12 and 3 appears as 15 (which is 12+3) instead of appearing as 123. It does that for every row in the weight column in a mysql table.

 

Near the end of the code (4th line up from end of code), I need $varw to go in as many rows as is necessary of a different mysql table. There is 1 $varw per row. $varw is still a number, but I'm not sure if it represented as a string after the numbers are added.  I have tried a few variations of foreach loops, and I assume that is what I need (?) but I still can't get it to work.  It currently adds 1 row each time the query is run. 

 

I'm not sure if this is part of the problem, but the first time the script is run (or if all the previous numbers are gotten rid of) it enters 0 for the first row (wrong).  After that it enters each row after the first as if the 2nd row was the first row.  For example if it should say 4 in the first row, it says 0 in the first row and 4 in the second row.

 

Here is the code in its original form: http://www.sonyjose.in/blog/?p=62 The array is obtained differently (manual entry) and it doesn't end up going to a database. I optimized the code using the examples in the comments on that page.

 

 

<form action="insert.php" method="post" name="weight">   
<?php           
$data = mysql_query('SELECT weight FROM myTable WHERE session_id = "' . session_id() . '"');   
$params = array();  
while ($row = mysql_fetch_assoc($data)) {       
$params[] = $row['weight'];   
}  
$combinations=getCombinations($params);  
function getCombinations($array) {      
$length=sizeof($array);      
$combocount=pow(2,$length);  
for ($i=1; $i<$combocount; $i++)      
{  
$binary = str_pad(decbin($i), $length, "0", STR_PAD_LEFT);         
$combination='';         
for($j=0;$j<$length;$j++)          
{              
if($binary[$j]=="1")                  
$combination+=$array[$j];          
}          
$combinationsarray[]=$combination;       
$varw = $combination;     
Print "<input type='hidden' name='Weight' value=" . $varw . ">";             
}      
return $combinationsarray;  
}   

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.