3

I get a string something like this, "voornaam, achternaam" I give that to a function.

When I used the string to get the data from the database.

mysql_query("SELECT."$string". FROM ...")

this all goes good.

Now. I want to give back the values that I selected from the DB.

I tried something like this.

<?php
function user_info($user_id, $fetch = "")
{
if (ctype_digit($user_id))
{
    if (empty($fetch))
    {
        $fetch = "*";
    }
    $query = mysql_query("SELECT ".$fetch." FROM accounts WHERE ID = '".$user_id."'");
    $data = mysql_fetch_assoc($query);
    $data['password'] = NULL;
}

if (empty($fetch))
{
    return $data;
}
else
{
    $fetch = explode(", ", $fetch);
    print_r($fetch);
    while($param = $fetch) {
        return $data[$param];
    }
}

//$item_result['created_by'] gives back a digit
user_info($item_result['created_by'], 'voornaam, achternaam')

Im stuck in the while loop.
I am not realy that good in the loops.
So i tried something and to me its seems logical.
But in some way it wont work.

6
  • 2
    Please show us your full and real code so we can see how an with which API and so on you fetch your data. Also where are you stuck? Commented Apr 29, 2015 at 8:35
  • From where you are getting $param? Commented Apr 29, 2015 at 8:43
  • But I still don't really see where you are stuck/ what isn't working as you want it to? Commented Apr 29, 2015 at 8:43
  • 1
    @sgtBOSE I think it fell from the sky :) Commented Apr 29, 2015 at 8:43
  • @Rizier123 It seems that Lenap is stuck with while loop Commented Apr 29, 2015 at 8:44

4 Answers 4

2

try change

  while($param = $fetch) {
    return $data[$param];
}

on foreach loop:

$temp = array();
foreach($featch as $key){
  $temp[$key] = (empty($data[$key]) ? ''  :  $data[$key] );
}
return $temp;

UPDATE

Or you can use for loop :

$temp = array();
for( $i =0; $i < count($fetch); $i++){
  $temp[$fetch[$i]] = (empty($data[$fetch[$i]]) ? ''  :  $data[$fetch[$i]] );
}
return $temp;

UPDATE 2

OR while loop

$temp = array();
while(count($fetch)){
  $key = array_shift($fetch);
  $temp[$key] = (empty($data[$key]) ? ''  :  $data[$key] );
}
return $temp;
Sign up to request clarification or add additional context in comments.

5 Comments

My boss wont use foreach. If it was to me to decide I already used foreach
@Lenap sorry, but I didn't understand what you wanted to tell. There are a lot of possibilities to make loop.
Im tryng one of these things;)
Used the foreach loop.
Well.. I used first a forloop and then foreach
2

It is working now!

What I have now is:

$fetch = explode(", ", $fetch);
    $temp = array();
    for ($i = 0; $i < count($fetch); $i++) {
        $temp[$fetch[$i]] = (empty($data[$fetch[$i]]) ? '' : $data[$fetch[$i]]);
    }
    foreach ($temp as $data) {
        echo $data . ' ';
    }

Comments

1

The problem is in -

$fetch = explode(", ", $fetch);
print_r($fetch);
while($param = $fetch) {
    return $data[$param];
}

$fetch is an array now and $param is not defined.

You can try this -

$fetch = explode(", ", $fetch);
$i = 0;
$newData = array();
while($i < count($fetch)) {
    $newData[$fetch] = $data[$fetch];
    $i++;
}
return $newData;

I would suggest to use foreach instead of while.

1 Comment

return $data[$fetch]; - will return only one value not all of them from $fetch array
0

Alternatively you could use the following if it HAS to be in a while loop

$data=array();
while (list($var, $val) = each($fetch)) {
        $data[$var]=$val;
}

2 Comments

From code in question = $data - is array from mysql, and $fetch is keys what should be reutrn. so this is not quiet correct code
Its an example of the another way of doing it. SO isnt a code writing service, you can read and adapt the solution to your own code.

Your Answer

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