Solved

Problem with php 'for' loop

Posted on 2011-03-10
6
230 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Read about why website design really matters in today's demanding market.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmakerā€¦
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

20 Experts available now in Live!

Get 1:1 Help Now