Issue storing formatted data into DB

Hey all,

Seems I get one issue fixed and I run into another I can’t solve after hours of researching and rewriting.

had this working by simply passing the data from one variable to another like so:

$CalcsSets = $DisplayCalcs;

without the need to use the loop inside the if() statement and it inserted the data without quotes but all of a sudden it’s stopped working and I’m not sure why (it only started showing last integer), so I went with the more complex code trying to get it to work again as shown below.

Here’s the complex code I’m working with:

for($i=1; $i<=$CalcSets; $i++){


$calculations = PerformCalc($min, $highest, $OperatorType);

echo 'Calculations performed for '.$SetText[$i];

  foreach ($calculations as $key => $DisplayCalcs) {
      echo $SetCalc[] = $DisplayCalcs.', ';  //stores calculations with ',' in 
                                            //array.
}

if($CalcSets == 1){

   for($i=0;$i<$CalcSets;$i++){
      $SetResults = $SetCalc[$i];
      echo '<strong>'.(string)$SetResults.'</strong>';              
   }
        DB_Insert($SetResults); 
}

What it’s supposed to do is insert values in the following format (1,2,3,4,5,) into the database in a VARCHAR row but now all it shows is the last integer with no comma. I originally wanted to just store the integers and not a comma but I couldn’t get it to display on the page with commas after each integer so I went this route as mentioned earlier.

I realize I’m probably going about this the wrong way, but if any of you know a much easier, and shorter, way to do what I want, I’d be extremely appreciative of the help.

Reason I’m doing it this way, is because on the results page, it needs to show in the format mentioned above and there is another conditional if() that allows the user to select which results they’d like to keep if $CalSets is >= 2.

FYI, I did check the DB row and it is still set to VARCHAR with a length of 10 at the moment.

Can you see a problem with the following:


if($CalcSets == 1){
   for($i=0;$i<$CalcSets;$i++){

And why that would only allow one iteration of the for loop?

Can you also take a look at your code and verify your end-braces ( } ) are in the correct places.

My eyes and brain are so racked with debugging, re-writing, debugging, re-writing and so on and so on.

I do see that and I did changed that, and yep, the braces are in the correct place. I just forgot to add the last two braces at the end.

All I’m trying to do is insert ALL of the data from $DisplayCalc into the database (which I’m using prepared statements to do) and I can’t get this to work. I realize the code above is faulty and doesn’t work, I’m trying various methods to get this to properly work and I can’t figure it out. :headbang: :lol:

Ok, I’m going to go back to square one and try to make my intention as clear as possible.

array1 = array(1, 2, 3, 4, 5, 6);

foreach(array1 as $key => $data){
   echo $data.',';

// will display 1,2,3,4,5,6 in browser.
}

    if(is_true == 1){

    INSERT $data values into DB here.

    }

That’s what I’m trying to accomplish in it’s simplest form, I’m just have extreme difficulty achieving my goal.

i guess you don’t want me to mention that you should never store more than one value in any column, right?

Lol, r937…That’s too funny. I love how you whacked the wasps’ nest with a smile :lol:

I know, but my intention is not to store a bunch of integers that I’ll need later on for the application, but rather a string that I can query and display. The values being stored in the DB are no longer of any consequence other than to display something back to the user and nothing more.

I did, however, find the solution to the problem. While everything, code wise, was working properly as it should, my DB field type was set to the wrong type apparently. Instead of VarChar it needed to be Text. I never knew that actually. I thought they both essentially did the same thing. Now I know.

Thanks for the help all. Your tips, tricks and advice does help me along my journey :slight_smile: