Jump to content

Storing formatted results in DB?


wolfcry

Recommended Posts

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

 

$CalcsSets = $DisplayCalcs;

 

without the need to use 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.

 

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

Link to comment
Share on other sites

Hi Muddy,

 

I have a strong feeling you're correct lol.

 

Well, the program is pretty complex as it stands but to summarize how the data is being input:

 

1. User inputs say 3 as their $min value, and 10 as their $highest value with an operator of '*' and a $CalcSet of say 4.

2. Program takes (3 * 6) and places those values into a random generating function and randomly outputs as many results as the user selected. So for this case, 4 is the $CalcSet so the output will display 4 different randomized calculations.

3. The resulting displayed sets will look like so:

    "Calculations performed for set 1 (2, 3 etc.): 23, 12, 45,  <...this will be displayed 4 times, each time with different values.

 

I was able to just pass the values from $DisplayCalcs to another $variable and it worked but now all it shows is the last integer. So I went a more complex route and it's proving to be quite a task.

 

I don't *really* want to store integers separated by commas in the DB (I can always cast to string and that's fine) but what I *really* want to do, is store the integers as one string and then format on display by placing a comma between each integer somehow because on a separate View page, their calculated set needs to be displayed in the format (int1, int2, int3, etc.)

 

Hopefully that helps to better clarify.

 

Link to comment
Share on other sites

Are you sure this data needs to be saved? Could you not just work on the current request?

 

I'm not sure what you mean, could you please elaborate?

 

As for the INSERT syntax, I'm using prepared statements and have that parameter bound by the String notation 's'.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Found out the issue, apparently the DB field has to be set to text rather than varchar in-order to insert strings like that. I never realized that. In either case, thanks for the help, those functions are working beautifully in different aspects of my application.

Link to comment
Share on other sites

Found out the issue, apparently the DB field has to be set to text rather than varchar in-order to insert strings like that. I never realized that. In either case, thanks for the help, those functions are working beautifully in different aspects of my application.

 

varchar and text can store the same kinds of data, there should be no issue there. The only thing I could see being an issue is the amount of characters allotted to varchar.

Link to comment
Share on other sites

That's what I thought, but it wouldn't work until I changed the field type to Text. For VarChar I had the length set to 18 and it would only accept 1 character. With Text, it's set to the same amount and now accepts them all. It is strange and I'm going to look into it more today when I get the chance. I'm running the latest PHP and MySQL versions so it shouldn't be doing this unless somehow it was configured wrong or was a bad install. Who knows, with technology it can be a million and one things lol.

Link to comment
Share on other sites

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.