Solved

Problem with php 'for' loop

Posted on 2011-03-10
6
234 Views
Last Modified: 2012-06-27
Hi,

I have a script like so:
<?php
if ($_POST['NC1']=='Prefix') {$NC1 = 'xx';} else $NC1 = $_POST['NC1'];
 if ($_POST['NC2']=='Chart number') {$NC2 = 'xx';} else $NC2 = $_POST['NC2'];
if ($_POST['NC3']=='Suffix') {$NC3 = 'xx';} else $NC3 = $_POST['NC3'];
if ($_POST['NC4']=='International number') {$NC4 = 'xx';} else $NC4 = $_POST['NC4'];
if ($_POST['NC5']=='Class') {$NC5 = 'xx';} else $NC5 = $_POST['NC5'];
if ($_POST['NC6']=='P,PandA,n/a') {$NC6 = 'xx';} else $NC6 = $_POST['NC6'];
if ($_POST['NC7']=='BA folio number') {$NC7 = 'xx';} else $NC7 = $_POST['NC7'];
if ($_POST['NC8']=='ARCS on cd') {$NC8 = 'xx';} else $NC8 = $_POST['NC8'];
if ($_POST['NC9']=='ARCS region') {$NC9 = 'xx';} else $NC9 = $_POST['NC9'];
if ($_POST['NC10']=='Chart title') {$NC10 = 'xx';} else $NC10 = $_POST['NC10'];
...
?>

Open in new window

But the thing is, I have over 100 lines like those above, so it's up to ($_POST['NC150']), I want to use 'for' loop here to make it easier, but not sure how to do that.
There's 4 'NC's per line, so normally if I replace all the 'NC' with 'for' loop element (e.g. $i), it wouldn't work, because in one line there will be NC1, NC2, NC3, NC4, and next line NC5, NC6 etc.
I want to have each NC to be NC1 in first line, NC2 in second line and so on, basically same as in php code above. I hope it's clear.

Thanks for any help.
0
Comment
Question by:Zado
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35093484
though you "could" update a variable with:

${NC . $loop} = $_POST["NC" . $loop];

in the end, you still have 150 variables instead of having 1 array ...
please rethink about that.

apart from that, you will need an array (indexed with the same "number" that holds the string to compare the $_POST value too, to make the loop "generic".

hope this helps
0
 
LVL 8

Author Comment

by:Zado
ID: 35093578
Could you help me to apply your idea into my code?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35093625
let's see:
<?php
// this is the "manual" part which cannot be done more generic, unless you store it in a database table/file and load it ...
$check = array();
$check["CC1"] = 'Prefix';
$check["CC2"] = 'Chart number';
// etc for the others


// now, fill over the final array
$NC = array()

for ($l = 1; $l<= 150; $l++)
{
  if (isset($_POST['NC'. $l]))
  {
     if ( $NC['NC'.$l] = $check['CC' . $l] )
    {
      // assign the dummy value
      // in case that should actually depend on the $l value somehow, 
      // you can create above a second array, exactly like $check, 
      // with the relevant matching values
      $NC['NC'.$l] = 'xx';
    }
    else
    {
      $NC['NC'.$l] = $_POST['NC' . $l];
    }
  }
  else // $post item is not set
  {
     $NC['NC'.$l] = '';
  }
?>

Open in new window


hope this clarifies
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Author Comment

by:Zado
ID: 35093805
Thanks
0
 
LVL 82

Expert Comment

by:hielo
ID: 35095269
>> if ( $NC['NC'.$l] = $check['CC' . $l] )
should be ==:

 if ( $NC['NC'.$l] == $check['CC' . $l] )

Full credit to angelIII
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35095381
indeed ! thanks for the correction
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question