using get to retrieve value in php - php

I want to retrieve the information from a table to another using the GET function. but it's not working. please, what am i doing wrong?.
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td><?php echo $row['year(date)'];?> </td>
</tr><?php }?>
the second page i want the the result to show in
$year = $_GET['year'];
$qry = "SELECT count(*), sum(basic), sum(hmo), sum(pension), sum(dha), sum(tax), sum(netpay), month(date) FROM salary WHERE year(date) ='$year' GROUP BY month(date)";
$run = mysql_query($qry) or die(mysql_error());
<?php while ($row = mysql_fetch_array($run)) {?>
<tr>
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td><?php echo $row['month(date)'];?> </td>
</tr><?php }?>

This will work.
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td>><?php echo $row['year(date)'];?> </td>
</tr><?php }?>
Reason: You were tried to pass value in month inside href. And tried to retrieve from get year parameter. Just change from month to year inside href. And you could able to retrieve year now. name parameter must to equal to get name parameter.

This is the URL in your link:
view_payroll_month.php?month=...
The query string parameter is called month. But you try to fetch a value called year:
$_GET['year']
Query string and/or form values are key/value pairs. The values are uniquely identified by the keys (names) with which they're associated. So you need to either:
Fetch the value by month instead of year, or
Pass the value year instead of month, or
Pass the value year in addition to month.
Something like this:
<a href="view_payroll_month.php?month=<?php echo $row['month(date)']; ?>&year=<?php echo $row['year(date)']; ?>">

Related

Php Loop TD if rows didn't exist or match

i want to loop some DTR values and their absent in PHP
As you can see the date should be 1,2,3,4,5 but since the only record they have is 3,4,5 there's some skipped rows, is it possible to loop empty TD and put missing date if there's no row exist? like i want to loop 1-31 then match the date then put the date where it belongs? idk dude
this is my code for my loop which basically just call the results of my query
<?php foreach ($report_stmt as $row): ?>
<tr>
<td><?php
$thisDate = $row['dtr_date'];
$day = strtotime($thisDate);
$newFormat = ltrim(date('d',$day),0);
echo $newFormat;
?></td>
<td><?php echo $row['dtr_in'] ?></td>
<td><?php echo $row['dtr_out'] ?></td>
<td><?php echo $row['dtr_in2'] ?></td>
<td><?php echo $row['dtr_out2'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
this is my query
$report_list = "SELECT * FROM tbl_dtr INNER JOIN tbl_users_info ON tbl_dtr.dtr_by = tbl_users_info.user_info_email WHERE dtr_by = :user AND dtr_date LIKE '%$month_filter%' ";
You can try to aggregate an array with the day of month as index. Then just loop through the day of months instead of loop through the query results.
Note: date with 'j' is better than trimming leading zero with 'd'. You may read the DateTime::format for the details of all formatting character.
<?php
// Aggregate an array of reports first
// Note: assume there is no 2 rows with the same 'dtr_date'
$reports = [];
foreach ($report_stmt as $row) {
$row['date'] = date('j', strtotime($row['dtr_date']));
$reports[$row['date']] = $row;
}
?>
<?php for ($i=1; $i<=31; $i++): ?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $reports[$i]['dtr_in'] ?? '' ?></td>
<td><?php echo $reports[$i]['dtr_out'] ?? '' ?></td>
<td><?php echo $reports[$i]['dtr_in2'] ?? '' ?></td>
<td><?php echo $reports[$i]['dtr_out2'] ?? '' ?></td>
</tr>
<?php endfor; ?>

Trying to get property of non-object using OCI connection

I'm new in PHP with OCI connection. I want to retrieve some data from database and insert it into a table form. But it keep show an error
Trying to get property 'attribute' of non-object
I have try to use oci_fetch object / oci_fetch_array, but it still the same. I also have followed some tutorial but it doesn't help.
This is for my mini project for this semester.
Here my source code:
$sql="SELECT borrow.book_id, book_title, borrow.stud_id, stud_name, book_bdate, return_date, due_date
FROM book
JOIN borrow
ON book.book_id = borrow.book_id
JOIN student
ON borrow.stud_id = student.stud_id
where borrow.stud_id = '$stud_id'
ORDER BY 5 DESC ";
$query=oci_parse($link,$sql) or die ("error here!");
oci_execute($query);
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row->stud_id; ?></td>
<td><?php echo $row->book_id; ?></td>
<td><?php echo $row->book_title; ?></td>
<td><?php echo $row->book_bdate; ?></td>
<td><?php echo $row->due_date; ?></td>
<td><?php echo $row->return_date; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row->book_id; ?>'>Update</a></center>
</td>
<tr>
<?php
}
}
oci_close($link);
?>
Oracle returns field names as uppercase by default so you need to use uppercase indexes like so:
Here the solution where I got.
Btw thank you everyone for helping me.
Correct Your while loop You are fetching data as array:
oci_fetch_array($query, OCI_ASSOC) // return associated array
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row['stud_id']; ?></td>
<td><?php echo $row['book_id']; ?></td>
<td><?php echo $row['book_title']; ?></td>
<td><?php echo $row['book_bdate']; ?></td>
<td><?php echo $row['due_date']; ?></td>
<td><?php echo $row['return_date']; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row['book_id']; ?>'>Update</a></center>
</td>
<tr>
<?php
}

How to display data in table based on the year in PHP

I want to display based on the year with repeated first_ ids and actual ids. but actual_id is unique.
<?php while($Row=oci_fetch_assoc($Result)){
$ResultArray[$j]['First_ID']=$Row['First_ID'];
$ResultArray[$j]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j]['Title']=$Row['Title'];
$ResultArray[$j]['START_DATE']=$Row['START_DATE'];
$startDate=explode(' ',$Row['START_DATE']);
$ResultArray[$j]['YEAR']=$startDate[0];
if(($startDate[0])==($startyear)){
$ResultArray[$j][$startyear]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
$j++;
}?>
//to display in table
it display year with asc order first and then next year etc.
<table class="tab">
<tr><th>S.No</th>
<th>first_ id</th><th>Title</th>
<?phpfor($i=0;$i<4;$i++){?><th colspan="3"><?php echo $startyear+$i; ?> </th>
<?php }?>
<tr><th></th><th></th><th></th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th> <th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th></tr>
<?php
foreach($ResultArray as $ResultArray1){
//print_r($abstarctResultArray1);
?><tr><td><?php echo $count; $count++;?></td>
<td><?php echo $ResultArray1['First_ID'];?></td>
<td><?php echo $ResultArray1['Title'];?></td>
<?php for($i=0;$i<4;$i++){
$startDate=explode(' ',$ResultArray1['START_DATE']);
if(($startDate[0]==$startyear)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
<?php }
else if(($startDate[0]==$startyear+1)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
else if(($startDate[0]==$startyear+2)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
}
}
?>
I have tried with different ideas, but I can't. Please help.

Only single record getting when i fetch data from another page

i have created a php page for fetch all data from mysql and i have another page to show data on webpage....
i have php page its like page_1.php
<?php include_once'db_localconnection.php';
$query="SELECT * FROM `table 5` where base='Home Plans'";
$get_allplans=mysql_query($query) or die(mysql_error());
while($fetch=mysql_fetch_array($get_allplans))
{
$plans_code=$fetch['plan_code'];
$speed=$fetch['speed'];
$data=$fetch['data'];
$duration=$fetch['duration'];
$gb_pm=$fetch['gb_pm'];
$up_speed=$fetch['up_speed'];
$price=$fetch['price'];
$base=$fetch['base'];
}
?>
and i have another page for show data i have also include all needed pages
<td><?php echo $plans_code; ?></td>
<td><?php echo $speed; ?></td>
<td><?php echo $data; ?></td>
<td class="center"><?php echo $duration; ?></td>
<td class="center"><?php echo $gb_pm; ?></td>
<td><?php echo $up_speed; ?></td>
<td><?php echo $price; ?></td>
<td>get reacharge</td>
so i am getting only first record please help..
You storing the values from db to variables i.e $plans_code, etc. in WHILE statement you just overwriting the values each time you loop. Instead, store them into array and send to your second page and display them.
example:
$completeData = array();
while($row=mysql_fetch_array($get_allplans))
{
array_push($completeData, $row);
}
now fetch the array $completeData to your second page and then display it,
like:
<?php foreach($completeData as $row) { ?>
<tr>
<td><?php echo $row['plans_code']; ?></td>
<td><?php echo $row['speed']; ?></td>
.
.
.
</tr>
<?php } ?>

How to make a PHP if statment dependent on SQL query results

I am trying to echo out the user level depending on whether the user level is either 1 or 5 based on SQL data results. Here:
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['id']; ?></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td><?php ?></td>
So I need a sort of if statement to select the user level from $rrows['id'] then if that selected data is 1 echo out "Network" and if it is "5" echo out "Administrator". How can this be done?
Seems like you need something like that:
$user_levels = array('Network','role2','role3','role4','Administrator');
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['id']; ?></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $users_levels[(int)$rrows['id']-1]; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td><?php ?></td>
The reason I am using -1 in the array is because the array is 0 based and your roles start with 1.
If you need to use the 'user_level' simply replace the row with
<td><?php echo $users_levels[(int)$rrows['user_level']-1]; ?></td>
Hope this helps!
I wonder how you got that far without an if statement but anyway, you also could do this right in you sql query. I always try to outsource as much logic to mysql as possible for a balanced load distribution.
SELECT level, IF(level = 1, 'Network', 'Administrator') as level FROM table
I think you can nest if statements to have more options.
Adapted from this answer: 'IF' in 'SELECT' statement - choose output value based on column values
I wouldn't do it with a if statement. I'd do it like that:
$levels = array(1 => "Network", 5 => "Administrator");
echo $levels[$rrows['user_level']];
That way, if you want to add other levels, you just add a value to the array and that's it.

Categories