3

I'm trying to separate a result into different arrays for each row, then output them into a table. Here is my code.. Its long I know and also outdated, but i just need it to work. Any help would be amazing. Currently it does echo the correct amount of rows, only they are all empty.

        <?php
        include ('user.inc');
        mysql_connect(localhost,$username,$password);
        @mysql_select_db($database) or die ("unable to select database");

            $query = 'SELECT * FROM customer LIMIT 50';
            $result=mysql_query($query);
            $num=mysql_numrows($result);

        mysql_close();

            $i=0;

            while ($i = mysql_fetch_array($result)){
                $customerID[$i]=$i['customerID'];
                $surname[$i]=$i['surname'];
                $forname[$i]=$i['forname'];
                $title[$i]=$i['title'];
                $email[$i]=$i['e-mail'];
                $adress[$i]=$i['address1'];
                $street[$i]=$i['street'];
                $city[$i]=$i['city'];
                $county[$i]=$i['cuunty'];
                $postcode[$i]=$i['postcode'];
                $phoneNumber[$i]=$i['phoneNumber'];
                $mobileNumber[$i]=$i['mobileNumber'];
            $i++;
            }
            $r=0;
            echo'<table>';
            while ($r < $num){
                echo'<tr>';
                echo'<td><form action="userinfo.php"><input type="hidden" name="customerID" value="';
                echo$customerID[$r];
                echo'"><imput type="submit" value="';
                echo$customerID[$r];
                echo'"></form></td>';
                echo'   <td>';
                echo$title[r];
                echo'</td>';
                echo'   <td>';
                echo$surname[r];
                echo'</td>';
                echo'   <td>';
                echo$forname[r];
                echo'</td>';
                echo'   <td>';
                echo$email[r];
                echo'</td>';
                echo'</tr>';
            $r++;
            }
            echo'</table>';
        ?>
1
  • 1
    You shouldn't be using the same variable for your counter as well as your associative array. Commented Mar 4, 2013 at 10:34

2 Answers 2

1

change this

$i=0;

        while ($i = mysql_fetch_array($result)){
            $customerID[$i]=$i['customerID'];
            $surname[$i]=$i['surname'];
            $forname[$i]=$i['forname'];
            $title[$i]=$i['title'];
            $email[$i]=$i['e-mail'];
            $adress[$i]=$i['address1'];
            $street[$i]=$i['street'];
            $city[$i]=$i['city'];
            $county[$i]=$i['cuunty'];
            $postcode[$i]=$i['postcode'];
            $phoneNumber[$i]=$i['phoneNumber'];
            $mobileNumber[$i]=$i['mobileNumber'];
        $i++;
        }

to

//$i=0;

        while ($i = mysql_fetch_array($result)){
            $customerID[]=$i['customerID'];
            $surname[]=$i['surname'];
            $forname[]=$i['forname'];
            $title[]=$i['title'];
            $email[]=$i['e-mail'];
            $adress[]=$i['address1'];
            $street[]=$i['street'];
            $city[]=$i['city'];
            $county[]=$i['cuunty'];
            $postcode[]=$i['postcode'];
            $phoneNumber[]=$i['phoneNumber'];
            $mobileNumber[]=$i['mobileNumber'];
        //$i++;
        }
Sign up to request clarification or add additional context in comments.

3 Comments

didnt work sorry, i got a server error instead. Thanks anyway.
your code contains lots of error. first correct them, after that it will work.
Thanks very much, great help. id left a second '}' in the code which must have f'ed something up. Thanks again :D
0

Why are you reading all the data in to an array and then just writing it out? This is unnecessary and wastes memory. Put your echo statements into the first loop with references to the relevant array nodes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.