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 } ?>
Related
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; ?>
Okay, my problem is that how can I set different hyperlinks to each row of a table in php?
so far i've done this -->
<?php
$conn = mysqli_connect("localhost", "root", "", "makaut_colleges");
$result = mysqli_query($conn, "SELECT * FROM collegelist");
while ($row = mysqli_fetch_assoc($result)):
?>
<tr>
<td><?php echo $row['College Name']; ?></td>
<td><?php echo $row['College Address']; ?></td>
<td><?php echo $row['College Code']; ?></td>
<td><?php echo $row['Review']; ?></td>
</tr>
<?php endwhile; ?>
How can i add different hyperlinks to each row which will be extracted from database? i'm using 'College Code' as primary key.
You can try something like this
Try this for making tr clickable
$dynamic_link = "dynamic.php";
<tr onclick="document.location = '<?php echo $dynamic_link ?>'">
td code---------
</tr>;
where $dynamic_link is dynamic based on your requirement
Thier should be some logic or linking with url either save in table or create some link
I have a table that is displaying data to the user and each row in the table has a unique id, in the table I am generating a button for each row to get more information on the content in the row. However, i need to generate a unique href for each button based on the Unique ID of the data in the row. The way I have done it it correctly goes to the link in the href but without passing the Unique ID.
<tbody>
<?php while($row = $query->fetch()) { ?>
<tr>
<td><?php echo $row['car_id']; ?></td>
<td><?php echo $row['car_make']; ?></td>
<td><?php echo $row['car_model']; ?></td>
<td><?php echo $row['number_available']; ?></td>
<td><?php echo $row['rent_cost_per_day']; ?></td>
<td><a class="btn btn-default" href="cars.php?"<?php $row['car_id']; ?>>View</a></td>
</tr>
<?php } ?>
</tbody>
Any help?
This is because you put the php after you closed the href quotes. You also have not put an echo function for the php, and have not put a get variable. You have:
href="cars.php?"<?php $row['car_id']; ?>
What you want to have is:
href="cars.php?id=<? echo $row['car_id']; ?>"
I want to display data in single foreach loop. I have two tables dailystats and monthlystats both of them have same columns like calls,minutes,incomingcalls etc Im using Yii PHP Framework Here is my controller
public function actionViewStats(){
$model = new Company();
$id = $_GET['id'];
$modelMonthly = $model->getCompanyUsageMonthly($id);
$modelDaily = $model->getCompanyUsageDaily($id);
$this->renderPartial('/shared/_company_stats', array(
'modelDaily' => $modelDaily,
'modelMonthly'=>$modelMonthly
));
}
Here is my view of table.
<?PHP if(isset($modelMonthly) && sizeof($modelMonthly)!=0): ?>
<div class="ibox-content col-md-12 col-xs-12">
<div class="title col-md-2">
<h3>Current Usage</h3>
</div>
<div class="col-md-10">
<div class="col-md-12 col-xs-12">
<table class="table">
<thead>
<th>Minutes</th>
<th>Incoming Minutes</th>
<th>Calls</th>
<th>Incoming Calls</th>
</thead>
<tbody>
<?PHP
if(isset($modelMonthly)){
foreach($modelMonthly as $monthlystats){
?>
<tr>
<td><?php echo $monthlystats->minutes?></td>
<td><?PHP echo $monthlystats->incoming_minutes; ?></td>
<td><?PHP echo $monthlystats->calls; ?></td>
<td><?PHP echo $monthlystats->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
</tbody>
</table>
</div>
</div>
</div>
<?PHP endif;?>
This is showing only monthly stats modelMonthly but i want to show daily stats modelDaily too in the same foreach loop.. How do i do that? I have tried array_combine etc but unable to do it. Searched on SOF but unable to find solution for it.
My code above shows only Monthly stats like this
But I want to show like this below. I have already made the table but im not able to use both modelDaily and modelMonthly in same foreach loop. I have tried different things but unable to do it..
<?PHP
if(isset($modelMonthly)){
foreach($modelMonthly as $usage){
?>
<tr>
<td><?php echo $usage->minutes?></td>
<td><?PHP echo $usage->incoming_minutes; ?></td>
<td><?PHP echo $usage->calls; ?></td>
<td><?PHP echo $usage->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
If both your arrays have numerical indexes, you can use a regular for loop:
for ($i = 0; $i < max(count($modelMonthly), count($modelDaily)); $i) {
if (isset($modelDaily[$i]) {
// Add the daily columns
} else {
// Add as much empty columns
}
if (isset($modelMonthly[$i]) {
// Add the monthly columns
} else {
// Add as much empty columns
}
}
The next step is adding the required headers th in the thead, and adding the extra td inside the loop.
Alternatively, you can create both table independantly, and position them using CSS. It is not in the scope of this question, but this is what I would recommend.
You shoudl use this tecnique
foreach($modelMonthly as $index => $usage){
?>
<tr>
<td><?php echo $usage->minutes?></td>
<td><?PHP echo $usage->incoming_minutes; ?></td>
<td><?PHP echo $usage->calls; ?></td>
<td><?PHP echo $usage->incoming_calls; ?></td>
<td><?php if (isset($dailystats[$index]->minute)) echo $dailystats[$index]->minutes?></td>
<td><?PHP if (isset($dailystats[$index]->incoming_minutes)) echo $dailystats[$index]->incoming_minutes; ?></td>
<td><?PHP if (isset($dailystats[$index]->calls)) echo $dailystats[$index]->calls; ?></td>
<td><?PHP if (isset($dailystats[$index]->incoming_calls)) echo $dailystats[$index]->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
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 } ?>