Forum Moderators: coopster

Message Too Old, No Replies

Change the Last Value from mysql fetch array

fetch array

         

Underdog76

12:54 am on Apr 13, 2010 (gmt 0)

10+ Year Member



Hello,

I need to change the last value of an array Basically here is the code:

$row = mysql_fetch_array($result);




$td5 = $row['property_id'];
$paxrate = $row['1person'];



if ($td5 =="$property_id")
{
echo "$";
echo $paxrate;
}

This basically shows all rows that are within the property_id, for instance I have 5 entries with the same property_id and 1person = 22 now I have a list of

$22
$22
$22
$22
$22

I need to change the last row to "Depart"

like:
$22
$22
$22
$22
Depart

Any Help would be great!

Readie

2:02 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World Underdog76

$count = count($someArray);
$count = ($count - 1);
$someArray[$count] = 'Depart';

Should do the trick.

Underdog76

2:13 am on Apr 13, 2010 (gmt 0)

10+ Year Member



I used:
$count = count($row);
$count = ($count - 1);
$row[$count] = 'Depart';

and still nothing, Could you be more precise based on the code I provided:

EX:
$row = mysql_fetch_array($result);




$td5 = $row['property_id'];
$paxrate = $row['1person'];

$count = count($row);
$count = ($count - 1);
$row[$count] = 'Depart';


if ($td5 =="$property_id")
{
echo "$";
echo $paxrate;
}

**** the above does not work, is there something I am missing?

Readie

2:16 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$count = count($row['1person']);
$count = ($count - 1);
$row['1person'][$count] = 'Depart';

mysql_fetch_array returns a multi-dimensional array :) (arrays within an array)

Underdog76

2:21 am on Apr 13, 2010 (gmt 0)

10+ Year Member



still nothing

Underdog76

2:24 am on Apr 13, 2010 (gmt 0)

10+ Year Member



heres the code with some extra to view

$query="SELECT property_id, 1person FROM rate_number WHERE property_id = '$property_id'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);




$td5 = $row['property_id'];
$paxrate = $row['1person'];

$count = count($row['1person']);
$count = ($count - 1);
$row['1person'][$count] = 'Depart';


if ($td5 =="$property_id")
{
echo "$";
echo $paxrate;
}

Underdog76

2:25 am on Apr 13, 2010 (gmt 0)

10+ Year Member



the above still echo's

$22
$22
$22
$22
$22

Readie

2:29 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$paxrate = $row['1person'];

Move this below the block of code I gave you :)

Underdog76

2:32 am on Apr 13, 2010 (gmt 0)

10+ Year Member



I changed it to:

$query="SELECT property_id, 1person FROM rate_number WHERE property_id = '$property_id'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);




$td5 = $row['property_id'];


$count = count($row['1person']);
$count = ($count - 1);
$row['1person'][$count] = 'Depart';
$paxrate = $row['1person'];

if ($td5 =="$property_id")
{
echo "$";
echo $paxrate;
}

**Now it shows this

$D2
$D2
$D2
$D2
$D2


....weird

Readie

2:39 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is wierd indeed :/

OK, let's try this in a different manner.

$query="SELECT property_id, 1person FROM rate_number WHERE property_id = '$property_id'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);

$td5 = $row['property_id'];
$paxrate = $row['1person'];
$count = count($paxrate);

if ($td5 == "$property_id")
{
for($i = 0; $i < $count; $i++) {
if($i < ($count - 1)) {
echo '&#36;' . $paxrate[$i] . '<br />';
} else {
echo 'Depart';
}
}
}

Underdog76

2:56 am on Apr 13, 2010 (gmt 0)

10+ Year Member



now it echo's
$2
$2
$2
$2
$2


let me give you the complete breakdown so you can see my true issue

I need a date range for instance $from = 2010-4-20
and $to = 2010-4-25

based on the property_id = 69

in the rate number table all property_id = 69 equal 1person = 22

I needed a horizontal array, not a vertical showing this output:

20 Apr 21 Apr 22 Apr 23 Apr 24 Apr 25 Apr Sub-Total
$22 $22 $22 $22 $22 Depart $110.00

so far I have it looking like so:

4/10 4/11 4/124/13 4/14 4/15
$22 $22 $22 $22 $22 $22

with the code below:


$result22 = mysql_query("SELECT * FROM rate_cal_tbl WHERE property_id = '$property_id' and calDate BETWEEN '$from' AND '$to' ORDER BY calDate")
or die(mysql_error());



// keeps getting the next row until there are no more to get
$display = round($days) + 1;
$cols = 0;
echo "<table>";
while($fetched = mysql_fetch_array($result22)){


if($cols == 0){
echo "<tr>\n";
}

// put what you would like to display within each cell here
echo "<td>".$fetched['month']."/".$fetched['day']."<br />";


include("*****.php");
$connect55=mysql_connect($server, $db_user, $db_pass)
or die ("Mysql connecting error");
mysql_select_db('********');


$query33="SELECT property_id, 1person FROM rate_number WHERE property_id = '$property_id'";
$result33 = mysql_query($query33) or die(mysql_error());
$row33 = mysql_fetch_array($result33);




$td5 = $row33['property_id'];
$paxrate = $row33['1person'];


if ($td5 =="$property_id")
{
echo "$";
echo $paxrate;
}

echo "&nbsp;&nbsp;</td>\n";
$cols++;
if($cols == $display){
echo "</tr>\n";
$cols = 0;
}

}
// added the following so it would display the correct html
if($cols != $display && $cols != 0){
$neededtds = $display - $cols;
for($i=0;$i<$neededtds;$i++){
echo "<td></td>\n";
}
echo "</tr></table>";
} else {
echo "</table>";
}



}


so basically where I am now is creating the depart in the last day of the array, making the sub total add the days before depart and changing the dates fro 4/20 to look like 20 Apr

and it all has to be horizontal, not vertical

thats the complete issue I am dealing with......

Readie

2:57 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm. Ok, could you run this command, and paste the output please? I think what we're dealing with here is me just not knowing enough:

print_r($paxrate);

Underdog76

3:00 am on Apr 13, 2010 (gmt 0)

10+ Year Member



22

Underdog76

3:01 am on Apr 13, 2010 (gmt 0)

10+ Year Member



replace the

echo $paxrate

=

$22

Readie

3:02 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As for the changing 4/20 to 20 Apr:

$somevariable = '4/20';
$pieces = explode("/", $somevariable);
echo date('j M', mktime(0, 0, 0, $pieces[0], $pieces[1]));

Readie

3:03 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you've written above cannot be the output for print_r($paxrate) :/

[php.net...]

Underdog76

3:04 am on Apr 13, 2010 (gmt 0)

10+ Year Member



yes but how to implement within the vert array:

echo "<td>".$fetched['month']."/".$fetched['day']."<br />";

Readie

3:07 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo "<td>".$fetched['month']."/".$fetched['day']."<br />"; 
echo '<td>' . $fetched['day'] . ' ' . date('M', mk_time(0, 0, 0, $fetched['month'])) . '<br />'; 

Underdog76

3:10 am on Apr 13, 2010 (gmt 0)

10+ Year Member



with this code:

$query="SELECT property_id, 1person FROM rate_number WHERE property_id = '$property_id'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);

$td5 = $row['property_id'];
$paxrate = $row['1person'];
$count = count($paxrate);

if ($td5 == "$property_id")
{
for($i = 0; $i < $count; $i++) {
if($i < ($count - 1)) {
echo '&#36;' . $paxrate[$i] . '<br />';
} else {
echo 'Depart';
}
}
}
print_r($paxrate);

Shows

Depart22 Depart22 Depart22 Depart22 Depart22

Underdog76

3:14 am on Apr 13, 2010 (gmt 0)

10+ Year Member



btw

Replacing:

echo "<td>".$fetched['month']."/".$fetched['day']."<br />";

with

echo '<td>' . $fetched['day'] . ' ' . date('M', mk_time(0, 0, 0, $fetched['month'])) . '<br />';

killed it

and this is not using the code:


$somevariable = '4/20';
$pieces = explode("/", $somevariable);
echo date('j M', mktime(0, 0, 0, $pieces[0], $pieces[1]));

Readie

3:24 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Argh, I'm too tired for this.

Are the multiple 22's on seperate MySQL table rows

I.E.

 - | 1person |
--------------
0 | 22 |
1 | 22 |
2 | 22 |


Or is it one cell, Like so:

- | 1person |
------------------
0 | 22 22 22 22 |


?

Underdog76

3:46 am on Apr 13, 2010 (gmt 0)

10+ Year Member



no, each entry based on a date
$from = $_GET['from'];
$to = $_GET['to'];
$property_id = $_GET['property_id'];

this table1 also has a calDate column = 2010-4-10 (all the way to 2010-4-15 and Property_id column = 69 in the same row

so look at it as multiple calDates with the same property_id

with the property_id as 69 it calls from another table2 with the same property_id and also with a column 1person =22 associated with that property_id

so column 1person with the property_id of 69 is 22

the array is showing the 1person column with property_id 69 from CalDate =$from to CalDate = $to

I Do appreciate all your help, I have been at this for 2days and its got me stumped......

so there is only 1 row with property_id 69 in table 2 and with 1person =22 in the same row

Underdog76

3:46 am on Apr 13, 2010 (gmt 0)

10+ Year Member



no, each entry based on a date
$from = $_GET['from'];
$to = $_GET['to'];
$property_id = $_GET['property_id'];

this table1 also has a calDate column = 2010-4-10 (all the way to 2010-4-15 and Property_id column = 69 in the same row

so look at it as multiple calDates with the same property_id

with the property_id as 69 it calls from another table2 with the same property_id and also with a column 1person =22 associated with that property_id

so column 1person with the property_id of 69 is 22

the array is showing the 1person column with property_id 69 from CalDate =$from to CalDate = $to

I Do appreciate all your help, I have been at this for 2days and its got me stumped......

so there is only 1 row with property_id 69 in table 2 and with 1person =22 in the same row

I Do appreciate all your help, I have been at this for 2days and its got me stumped......

Underdog76

4:11 am on Apr 13, 2010 (gmt 0)

10+ Year Member



if

$count = count($paxrate);

echo $count Shows

$1 $1 $1 $1 $1

so its showing row 1person 1 time in each date

question is, how to get the last entry in the array to show Depart?

Underdog76

6:36 am on Apr 13, 2010 (gmt 0)

10+ Year Member



I resolved the issue:

Here is the fix I have implemented Compare with my previous code

The Depart issue, Month, and subtotal

$result22 = mysql_query("SELECT * FROM rate_cal_tbl WHERE property_id = '$property_id' and calDate BETWEEN '$from' AND '$to' ORDER BY calDate")
or die(mysql_error());



// keeps getting the next row until there are no more to get
$display = round($days) + 1;
$cols = 0;
echo "<table>";
while($fetched = mysql_fetch_array($result22)){
//helps fix depart issue//
$td6 = $fetched['calDate'];

//helps fix month issue//
$month1 = $fetched['month'];
$monthName = date("F", mktime(0, 0, 0, $month1, 10));


// put what you would like to display within each cell here
echo "<td>".$fetched['day']."&nbsp;".$monthName."&nbsp;&nbsp;<br /><center>";


include("****.php");
$connect55=mysql_connect($server, $db_user, $db_pass)
or die ("Mysql connecting error");
mysql_select_db('********');


$query33="SELECT property_id, 1person FROM rate_number WHERE property_id = '$property_id'";
$result33 = mysql_query($query33) or die(mysql_error());
$row33 = mysql_fetch_array($result33);

//this fixes the depart issue//
if ($td6 =="$to")
{
echo "<font size='2' face='Verdana' color='#FF3A00'>Depart</font>";
}



$td5 = $row33['property_id'];
$paxrate = $row33['1person'];

//fixes double depart entry//
$dirty = 'Depart';
$clean = array_unique($dirty);


if ($td6 =="$to")
{
echo "$clean";
}
else
{
echo "$";
echo $paxrate;
}

echo "&nbsp;&nbsp;</center></td>\n";
$cols++;
if($cols == $display){
echo "</tr>\n";
$cols = 0;
}

}
// added the following so it would display the correct html
if($cols != $display && $cols != 0){
$neededtds = $display - $cols;
for($i=0;$i<$neededtds;$i++){
echo "<td></td>\n";
}
echo "</tr></table>";
} else {
echo "</table>";
}



}






echo "<td><center><font size='2' face='Verdana' color='#00335A'><b>Sub- Total</b></font><br>";
$remdisplay = $display - 1;
$subtotal = $paxrate * $remdisplay;
echo "<font size='2' face='Verdana' color='#00335A'><b>$";
echo "$subtotal.00</b></font><br>";
echo "<font size='2' face='Verdana' color='#00335A'><b><a href='taxandfee.php' target='blank' >Taxes / Fees</a></font></b></center>";
echo "</td></td>";
echo "</tr>
</table></td>
</tr>
<tr>
<td>";

Readie

11:30 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My apologies for the cessation of my replies, I went to sleep :)

Well done, I'm glad you got this fixed in the end.

Underdog76

1:04 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



All good I do appreciate your assistance:)