Solved

Problem with php 'for' loop

Posted on 2011-03-10
6
232 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now