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.
Related
fetching some data from mysqli
$value=mysqli_fetch_assoc($query)
I have a result like
$value = "123456789101112131415161718192021222324252627282930";
but I want to use it two variables like
$value1 = "123456789101112131415";
$value2 = "161718192021222324252627282930";
Use the database column names.
If you don't have the value you need in separate database column you have to somehow extract the value from an existing column.
You can use substr or regex to do so.
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['DATABASE_COLUMN_1_NAME']; ?></td>
<td><?php echo $row['DATABASE_COLUMN_2_NAME']; ?></td>
<td><?php echo $row['DATABASE_COLUMN_3_NAME']; ?></td>
<!-- show only a part of the db columns content -->
<?php $valueIwantToShow = subtr($row['DATABASE_COLUMN_4_NAME'], 0, 5); ?>
<td><?php echo valueIwantToShow; ?></td>
<!-- or show multiple values in one html column -->
<td><?php echo $row['DATABASE_COLUMN_6_NAME']; ?>, <?php echo $row['DATABASE_COLUMN_7_NAME']; ?></td>
</tr>
<?php } ?>
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
}
I have table that contain data
<td><?php echo $users->aa_0?></td
<td><?php echo $users->aa_1?></td>
<td><?php echo $users->aa_2?></td>
<td><?php echo $users->aa_3?></td>
when i'm applying static value to above code, it is working
But when i'm changing it to dynamic the below code is not working...
kindly help me
<?php foreach ($get_users as $users) { ?>
<tr>
<td><?php echo $users->aa_.$i?></td>
<?php ?>
</tr>
<?php } ?>
Basically
$users->{"aa_$i"}
But as JJ said, you should use arrays, like
$users->aa[$i]
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 } ?>
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)']; ?>">