Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with php 'for' loop

Posted on 2011-03-10
6
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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 143

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

722 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