Solved

PHP Loop

Posted on 2013-11-18
2
251 Views
Last Modified: 2014-04-22
need code to do the following in php

Specify a max nbr to loop thru like 1 thru 1000
Specify a start value for index any numeric value
Each time index should increase by 1
2nd value should be a random 10 digit nbr

the output needs to look like this

$cb->set("index", $randomnumber);
0
Comment
Question by:AlexPonnath
  • 2
2 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39657771
<?php // RAY_temp_alexponnath.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28297482.html

/**
 * Specify a max nbr to loop thru like 1 thru 1000
 * Specify a start value for index any numeric value
 * Each time index should increase by 1
 * 2nd value should be a random 10 digit nbr
 * the output needs to look like this: $cb->set("index", $randomnumber);
 */

// THE CLASS DEFINITION FOR "$cb"
Class Cb
{
    public function set($index, $randomnumber)
    {
        $this->data[$index] = $randomnumber;
    }
}

// INSTANCE OF THE CB CLASS
$cb = new CB;

// RANGE OF TEN DIGIT NUMBERS
$lo = 1000000000;
$hi = 9999999999;

// NUMBER OF ELEMENTS TO CREATE
$lim = 32;

// STARTING INDEX
$ndx = 16;

// THE ARRAY KEYS
$arr = range($ndx, $ndx+$lim);
foreach ($arr as $key)
{
    $cb->set($key, rand($lo, $hi));
}

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($cb);

Open in new window

Best regards, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40016129
@AlexPonnath: Please see the EE grading guidelines:
http://support.experts-exchange.com/customer/portal/articles/481419

Out of your last ten questions, you've given only one grade of "A" -- a behavior that is astonishingly out of step with an online community where the default and expected grade is "A" unless the responses are deficient.

In the instant case you got a tested and working code example that does exactly what you asked for, and you got it within 15 minutes of posting the question.  So I think you owe an explanation for the marked down grade.  Specifically, what were you expecting that you did not get?

Standing by to hear back from you! ~Ray
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

Title # Comments Views Activity
Echo images using file system 2 32
is this a cms? 8 35
Intermittent Error on Page Loading 4 19
PHP: Insert Data into MySQL 5 16
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 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

13 Experts available now in Live!

Get 1:1 Help Now