#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Streamlining and add an incrementing # variable


    Here is part of my code, what I am wondering is, how do I streamline this (there are many more lines that I cropped for this example) - also, I was playing with using $i++ for the numberals (00, 01, 02, etc.), but I keep getting errors when trying to insert it to my variables below... thanks for any help offered!


    PHP Code:

    // LOOP - SET SERVICE INFORMATION - START
    while( $row3 sqlsrv_fetch_array$stmt3SQLSRV_FETCH_ASSOC) ) {                  

    // SET PARAMETERS - SET SERVICES INFORMATION
    $id_division_00='d'.trim($row3["id_division_00_translated_calc"]);
    $id_division_01='d'.trim($row3["id_division_01_translated_calc"]);

    $divisions_services_00_description=$row3["divisions_services_00_description"];
    $divisions_services_01_description=$row3["divisions_services_01_description"];

    $id_module_001='m'.trim($row3["id_module_001"]);
    $id_module_002='m'.trim($row3["id_module_002"]);
    $id_module_003='m'.trim($row3["id_module_003"]);

    $svc_module_001=trim($row3["svc_module_001"]);
    $svc_module_002=trim($row3["svc_module_002"]);
    $svc_module_003=trim($row3["svc_module_003"]);

    // SET SESSIONS - SET SERVICES INFORMATION
    $_SESSION['id_d00']=$id_division_00;
    $_SESSION['id_d01']=$id_division_01;

    $_SESSION['divisions_services_00_description']=$divisions_services_00_description;
    $_SESSION['divisions_services_01_description']=$divisions_services_01_description;

    $_SESSION['id_m001']=$id_module_001;
    $_SESSION['id_m002']=$id_module_002;
    $_SESSION['id_m003']=$id_module_003
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,337
    Rep Power
    594
    Perhaps you need to restructure your database. It appears to me that those column names are the source of unnecessary complexity. In any case you did not show how you tried to use $i++ so there is no way to help you with the errors.

    Comments on this post

    • Jacques1 agrees
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    do you actually need to have all those values in separate variables? If you just wanna store them in the session, you could already kill the upper half of the code.

    And I agree with gw1500se that it's mainly an issue of poor database design. Groups of repeated data (module_1, module_2, module_3, ...) are always a bad sign. It means your database isn't normalized.

    If you have a chance to normalize your database, do it. You'll then be able to fetch the data in groups instead of one by one.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Streamlining and incrementing


    The database structure must stay the same, I am not able to change that. So I must provide code. I have tried the following, I am providing the first part of my while statement, but am getting errors - I may be way off in my approach...

    Here are the errors:
    Notice: Undefined index: id_division_0++_translated_calc in D:\Inetpub\wwwroot\pwf\common\pwfsessions.php on line 180

    Notice: Undefined index: divisions_services_1++_description in D:\Inetpub\wwwroot\pwf\common\pwfsessions.php on line 181

    I will keep working on it, but if anyone has thoughts they are very appreciated!

    PHP Code:
    $i=str_pad('-1'2'0'STR_PAD_LEFT);
    $idv='$id_division_';
    $idv1='$divisions_services_';
    $idv2='_description';

    // SET PARAMETERS - SET SERVICE INFORMATION
    ${$idv.$i++}='d'.trim($row3["id_division_$i++_translated_calc"]);
    ${
    $idv1.$i++.$idv2}=$row3["divisions_services_$i++_description"
    Last edited by laural4705; March 6th, 2013 at 11:21 AM. Reason: Adding Information
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    You cannot do an increment within a string. I'm not even sure what you're trying to do. An increment increases a variable by 1 (hence the name), it's not a loop or something. When you have four $i++, you get four different numbers -- that's obviously not what you want.

    You could theoretically do the repetitions with for loops. But this also comes with an overhead, so it's not necessarily worth it.

    What's much more important is what I already asked you: Do you actually need to have each value in a separate variable? Because you could get rid of half of the code if you didn't flood the scope with all those crappy variables "$..._1", "$..._2", ....

    You should also consider grouping the values in your PHP code. How about arrays? The database structure is sh*t, but that doesn't mean you have to use the same structure in your code.
    Last edited by Jacques1; March 6th, 2013 at 12:05 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,337
    Rep Power
    594
    There are several problems. Regardless of how you initialize $i, it will be changed to an integer type when you use it as one. Also, each $i++ increments the variable so you are in effect doing this:
    PHP Code:
    ${$idv.0}='d'.trim($row3["id_division_1_translated_calc"]); 
    ${
    $idv1.2.$idv2}=$row3["divisions_services_3_description"
    Which I doubt is what you want even if your idea worked. Although you say you cannot change the database structure, that does not mean you cannot change the PHP variables to a meaningful array. You might also consider using an indexed array rather than an associative array when you get the rows. You might need to do multiple queries to get everything in a more sensible structure in PHP. That will be less efficient but might be worth the sacrifice.

    As a last resort, to get the keys you want you would need to write a function that returns the value of $i as an appropriately formatted string and concatenate (.) it properly in the key string.
    PHP Code:
    ...$row3["id_division_".makeString($i)."_translated_calc"]... 
    Last edited by gw1500se; March 6th, 2013 at 12:08 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Thank you


    I appreciate all of the advice, I will use this to formulate some new code - thank you!

    Originally Posted by gw1500se
    There are several problems. Regardless of how you initialize $i, it will be changed to an integer type when you use it as one. Also, each $i++ increments the variable so you are in effect doing this:
    PHP Code:
    ${$idv.0}='d'.trim($row3["id_division_1_translated_calc"]); 
    ${
    $idv1.2.$idv2}=$row3["divisions_services_3_description"
    Which I doubt is what you want even if your idea worked. Although you say you cannot change the database structure, that does not mean you cannot change the PHP variables to a meaningful array. You might also consider using an indexed array rather than an associative array when you get the rows. You might need to do multiple queries to get everything in a more sensible structure in PHP. That will be less efficient but might be worth the sacrifice.

    As a last resort, to get the keys you want you would need to write a function that returns the value of $i as an appropriately formatted string and concatenate (.) it properly in the key string.
    PHP Code:
    ...$row3["id_division_".makeString($i)."_translated_calc"]... 

IMN logo majestic logo threadwatch logo seochat tools logo