0

I have a site were the user fills a form and all data is stored in a database, when the user enter his/hers page all the added data is visible. Today I´m doing this but in a lot of code rows and there is for sure a much smoother way to do this.

Here´s a look of how I have done it today:

 $query = mysqli_query($dbhandle, "SELECT * FROM ..."); // ... added now
 $row = mysqli_fetch_assoc($query);

 $m0 = $row['m1'];
 $m1 = $row['m2'];
 $m2 = $row['m3'];
 $m3 = $row['m4'];
 ...
 $m47 = $row['m48'];
 $firstPlace = $row['firstPlace '];
 $secondPlace = $row['secondPlace '];
 $thirdPlace = $row['thirdPlace '];
 $fourthPlace= $row['fourthPlace'];

As you can see there are a lot of rows of code. What I would like to do is to loop through my query and then add the right value in the database to the right value in the form.

Appreciate help.

2
  • Help is here: stackoverflow.com/questions/17056349/… Commented Jun 30, 2014 at 11:18
  • You mean you want to assign each row column two a variable with the same name? Commented Jun 30, 2014 at 11:18

2 Answers 2

0

There definitely are many alternative (and in every possible sense of the word) better ways to go about your business.
For a kickoff: ask yourself what an array actually is. An array is a collection of data. You store them together because one value of that array in itself doesn't mean much. The data in an array belongs together. Why then, assign it to individual variables in the first place?

Of course, your $row array has keys like $row['m1'], which you assign to a variable called $m0. so the names of the fields in the database don't quite match the names your code uses. That's something that you can, quite easily, fix by changing your query: use aliasses for those fields:

SELECT m1 as m0, ... FROM 

Now your array will have a key called m0, instead of m1. This reduces the rest of your code down to:

$row = mysqli_fetch_assoc($query);
echo 'M0: ', $row['m0'];//<-- use m0 value here.

Alternatively, you could use a second array that maps these field-names to the name you want to use in your code:

$map = array(
    'm0' => 'm1'
);
echo 'M0: ', $row[$map['m0']];//use value of m0, which is the actual key if the $row array

Still, if you are hell-bound on unmaintainable, messy, error-prone and just awful code, you could use variable variables:

foreach ($row as $key => $value)
{
    $$key = $val;
}

Note the double $ in $$key. This is like saying "the variable that is called whatever the value of $key is". If $key is firstname, the code above evaluates to $firstname = $value. But whatever you do: forget this is possible. It's like an enema: yes, it's possible, but you don't want one if you can avoid it. And in this case, you clearly can avoid it.

Sign up to request clarification or add additional context in comments.

2 Comments

This helped, I changed the names in the db and then I only used $row['m0']; instead of creating a new variable and store the value Thanks!
@AwmaN: You didn't even have to change the field names in the DB, setting custom names in a query works equally well: SELECT field_name AS use_name works just fine.
0

Loop through the $row var grabbing the key and value. If key starts with "m" followed by a 1 or 2 digit number, get the number, subtract one, concatenate it with "m", and assign the value. Otherwise just interpolate key into variable name and assign value.

foreach ( $row as $key => $value ) {
    if ( preg_match('/^m(\d{1,2})/', $key, $matches) ) {
        ${'m' . ($matches[1] - 1)} = $value;
    }
    else { $$key = $value; }
}

In the above example, $row['m1'] value gets assigned to var $m0, and $row['firstPlace'] to var $firstPlace, etc.

1 Comment

regex + variable variables. Are you serious? Why doesn't anybody even think about SELECT field AS alias? Or a simple $map array, as I explained. Both of which are better options than what you suggest here