wolfcry Posted January 10, 2012 Share Posted January 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2012 Share Posted January 10, 2012 I have a very strong feeling you are way off tack on this. Could you break down where the info is coming from and how it is populated? Also why you need to store it in this format and what you want to do with it once it recovered. Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 10, 2012 Author Share Posted January 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 10, 2012 Share Posted January 10, 2012 Are you sure this data needs to be saved? Could you not just work on the current request? Anyway, if you are storing this is a varchar you need quotes in the value. INSERT INTO table (data) VALUES ('1,2,3,4,5') Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 10, 2012 Author Share Posted January 10, 2012 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'. Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 10, 2012 Author Share Posted January 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 10, 2012 Share Posted January 10, 2012 The right way would be to store each value in its own record, but if you want to break normalization and store all the values in a single field, you could implode it. Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 10, 2012 Share Posted January 10, 2012 If you want to store the array, use serialize() Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 10, 2012 Author Share Posted January 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 11, 2012 Share Posted January 11, 2012 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. Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 11, 2012 Author Share Posted January 11, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.