Jump to content

Recommended Posts

Hi there,

 

I am trying to do the following, but getting confused along the way (php / mysql newbie):

 

I have 2 queries - one from this month and one from last month. I need to show last months values as a tool tip (or perhaps as a second row) for this month (in order to be able to compare the values)

 

Here is my current code - I am aware where the mistake is - but no idea how to fix it (i think th emistake is that there is no relationship between the 2 queries):

 

query for this month:

$qryArea = "SELECT * FROM payments WHERE area='$area' AND month='$month' ORDER BY ag_nr";
	$resultArea = mysql_query( $qryArea );

 

query for last month:

 $qryAreaLast = "SELECT * FROM payments WHERE area='$area' AND month='$previous' ORDER BY ag_nr";
	$resultAreaLast = mysql_query( $qryAreaLast );

 

data extraction from q this month:

while($row = mysql_fetch_array($resultArea))
{
$agNr  = $row['AG_nr'];
$id      = (isset($row['id']))? $row['id'] : "";
$Size   = (isset($row['Size']))? $row['Size'] : "";
        }

 

data extraction from q last month:

while($rowLast = mysql_fetch_array($resultAreaLast))
{
$agNrLast      = $rowLast['AG_nr'];
$idLast          = (isset($rowLast['id']))? $rowLast['id'] : "";
$SizeLast = (isset($rowLast['Size']))? $rowLast['Size'] : "";
?>

// and lastly - here is the part where I TRY to do what I need:

<input type="text" name="Size" id="<?php echo 'Size'.$id; ?>" title="<?php echo $SizeLast ?>" value="<?php echo $Size ?>" />

<?php 
}
?>
</code>

At the moment, the $SizeLast is the simply repeated for each of the $Size - in other words, it is exactly the same on each $Size -

I think I need to say something like -> title=" $SizeLast where $agNrLast = agNr " but am unsure where to do this - in the mysql query (by having one query run, then somehow separating the months) or in the php

There are more than just one column I want to do this on - it's not just the Size column.

Any help greatly appreciated

Kindest

AikenD

Link to comment
https://forums.phpfreaks.com/topic/182805-how-to-mix-array-values/
Share on other sites

Hi AikenDrum,

 

You'd probably be best creating two arrays, indexed by the common value for each (i'm guessing ID, but this could be date?) and calling from each, such as:

 

<?php

// Initialise arrays
$resultsThisMonth = array();
$resultsLastMonth = array();

// Loop through first result set
while ($row = mysql_fetch_array($resultArea)) {
    // Add row to array by using unique value, shared by both result sets, as the index
    $resultsThisMonth[$row['id']] = $row;
}

// Loop through second result set
while ($row = mysql_fetch_array($resultAreaLast)) {
    // Add row to array by using unique value, shared by both result sets, as the index
    $resultsLastMonth[$row['id']] = $row;
}

// Loop through first array again to output HTML
foreach ($resultsThisMonth as $id => $row) {
    echo '<input type="text" name="Size" id="Size' . $id . '" title="' . $resultsLastMonth[$id]['Size'] . '" value="' . $row['Size'] . '" />
}

?>

 

If you don't need the full row with each ID (for instance, you just want the size), then you could use:

 

<?php

// Initialise arrays
$resultsThisMonth = array();
$resultsLastMonth = array();

// Loop through first result set
while ($row = mysql_fetch_array($resultArea)) {
    // Add row to array by using unique value, shared by both result sets, as the index
    $resultsThisMonth[$row['id']] = $row['Size'];
}

// Loop through second result set
while ($row = mysql_fetch_array($resultAreaLast)) {
    // Add row to array by using unique value, shared by both result sets, as the index
    $resultsLastMonth[$row['id']] = $row['Size'];
}

// Loop through first array again to output HTML
foreach ($resultsThisMonth as $id => $size) {
    echo '<input type="text" name="Size" id="Size' . $id . '" title="' . $resultsLastMonth[$id] . '" value="' . $size . '" />
}

?>

 

I'm guessing a lot at the purpose of this, but hopefully that should help somewhat. If you're stuck approaching it one way, try going back to the beginning, and write out in plain english what it is your trying to do, then convert the plain english to php.

 

iSE

Hi there,

 

many thanks for the speedy reply -

 

when you say "indexed by the common value for each (i'm guessing ID, but this could be date?) and calling from each " - do you mean "using a value that each array has in common" ?

 

They share an ID -  it is not the row ID , but another ID ( seen as $agNr  = $row['AG_nr']; )

 

Would I be able to use this ?

As in :

 

while ($row = mysql_fetch_array($resultArea)) {
    // Add row to array by using unique value, shared by both result sets, as the index
    $resultsThisMonth[$row['agNr']] = $row;
}

 

If so,

it seems relatively simple :-)

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.