Jump to content

Assigning a variable ...


aztec

Recommended Posts

Hello

 

Using PHP and MySQL I am getting the data I need from the database.

 

Sometimes it can be 2 rows and sometimes 17 rows.

 

I have a need to assign a different variable to each row when I iterate through the array.

 

Is this possible to do automatically using PHP.

 

I have hard coded this into a test page but this is very 'messy' and you have to put in the correct number of variables and multiple 'if' statements to achieve it, plus you need to know how many rows there are so you can put in the correct number of variables, and each call is different.

 

Kind Regards

 

 

Link to comment
Share on other sites

Hello

 

The webpage I am constructing uses the name returned and converts it into a hyperlink.

 

So I need to have precise control over what goes where on the page

$x=4;

while ($result = mysql_fetch_array($results))

{  
$id = $results['id'];  
  
if ($x == 4)  {$row1 = ($result);}

if ($x == 5)  {$row2 = ($result);}

$x++;

    

}

echo $row1 [first_name] ." " . $row1  [surname]; ?><br />
<?php echo $row2 [first_name] . " " .$row2 [surname];

?>

 

Regards

 

Link to comment
Share on other sites

I'm not sure what you mean. The code i provided dumps all results into an array, organized by the order the rows were returned from the query.

 

In order to have custom results, you have to have custom code. The more complex the results, the more complex the code.

 

If you want more detailed help, please provide a more detailed request or question.

Link to comment
Share on other sites

Hello discomatt

 

Thanks for your reply. Looking at what it produces is quite complex, but it does provide me one answer.

 

Could I ask you how would you extract say the second row, field 2.

 

At this stage of knowledge that would help me to get to grips with extracting the data I need.

 

Regards

Link to comment
Share on other sites

Well, using mysql_fetch_assoc returns column names for array keys, so that would be cumbersome... An easier method would be to change it to this:

 

$q = 'SELECT * FROM `table`';
$r = mysql_query($q);

while($d = mysql_fetch_row($r) )
   $a[] = $d;

print_r($a);

 

You would access rows and fields using their respective places - 1

 

so, 2nd row, 2nd field would be

$a[1][1];

 

5th row, 3rd field would be

$a[4][2];

 

ect.

Link to comment
Share on other sites

Hello discomatt

 

Thanks for your reply. Looking at what it produces is quite complex, but it does provide me one answer.

 

Could I ask you how would you extract say the second row, field 2.

 

At this stage of knowledge that would help me to get to grips with extracting the data I need.

 

Regards

 

If you only want a specific portion of the results than your where portion of the query is either non existent or too loose

Link to comment
Share on other sites

Hello Discomatt

 

Thanks a lot for your help and your code, you have moved my project forward my a considerable amount.

 

I can now report that I can get the exact data I need and use it in the way I want.

 

Once again thanks

 

Kind Regards

 

The topic is now resolved

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.