Jump to content

Can you use a FOR loop for this simple thing


wilsonf1
 Share

Recommended Posts

Ok so I have 8 database fields for 8 numbers. I also have 8 tags in my HTML for those numbers. I was going to use a FOR loop to loop through the tags, and replace each with the correpsonding db field, but I couldn't get it to work so now have 8 lines like so:

	 $this->Notes1HTML = str_replace("<Number1>", $this->formatPhoneNumber($this->directoryDataDB->Number1), $this->Notes1HTML);	 $this->Notes1HTML = str_replace("<Number2>", $this->formatPhoneNumber($this->directoryDataDB->Number2), $this->Notes1HTML);	 $this->Notes1HTML = str_replace("<Number3>", $this->formatPhoneNumber($this->directoryDataDB->Number3), $this->Notes1HTML);	 $this->Notes1HTML = str_replace("<Number4>", $this->formatPhoneNumber($this->directoryDataDB->Number4), $this->Notes1HTML);	 $this->Notes1HTML = str_replace("<Number5>", $this->formatPhoneNumber($this->directoryDataDB->Number5), $this->Notes1HTML);	 $this->Notes1HTML = str_replace("<Number6>", $this->formatPhoneNumber($this->directoryDataDB->Number6), $this->Notes1HTML);	 $this->Notes1HTML = str_replace("<Number7>", $this->formatPhoneNumber($this->directoryDataDB->Number7), $this->Notes1HTML);	 $this->Notes1HTML = str_replace("<Number8>", $this->formatPhoneNumber($this->directoryDataDB->Number8), $this->Notes1HTML);

Say I was looping round with $i being my counter, I could use:

"<Number" . $i ".>"

But I couldn't replace it with

$this->directoryDataDB->Number . $i

could I? Is there anyway round it or shall I stick with my 8 liner

Link to comment
Share on other sites

$num="number$i";$this->directoryDataDB->$num;

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...