SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    85
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    $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:

    Code:
    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.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can you see a problem with the following:
    PHP Code:
    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.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    85
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    85
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, I'm going to go back to square one and try to make my intention as clear as possible.

    Code:
    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.

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,260
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by netgrubber View Post
    ...debugging, re-writing, debugging, re-writing and so on and so on.
    i guess you don't want me to mention that you should never store more than one value in any column, right?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    85
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Lol, r937...That's too funny. I love how you whacked the wasps' nest with a smile

    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •