how do i print data vertically using loops - php

I have following problem .
I print data from a table name exam having student_id and marks_obtained for each student_id and. My output is as follow
but i want the table in different format
-----------------------
subject | marks | total
-----------------------
toward down side
as this see output
but am confused in code please help me
my CODE IS FOLLOW:
$roll=$_POST['roll'];
$exam=$_POST['exam'];
$int1 = intval(preg_replace('/[^0-9]+/', '', $exam), 10);
$student_info="select student_id,name,father_name,mother_name,roll,class_id,birthday,parent_id from student where roll='$roll'";
$result_student=mysqli_query($link, $student_info) OR trigger_error('login info error');
$data_student= mysqli_fetch_array($result_student,MYSQL_BOTH);
$parent_info="select name from parent where parent_id='$studentparent'";
$result_parent=mysqli_query($link, $parent_info) OR trigger_error('login info error');
$data_parent= mysqli_fetch_array($result_parent,MYSQL_BOTH);
$parentname= $data_parent['name'];
$subject_info="select subject_id,name from subject where class_id='$studentclass'";
$result_subject=mysqli_query($link, $subject_info) OR trigger_error('login info error');
$data_subject= mysqli_fetch_array($result_subject,MYSQL_BOTH);

<table>
<tr>
<td> Subject </td>
<td> Marks </td>
<td> Total </td>
</tr>
<tr>
<?php
for($i=0;i<count($data);i++){
?>
<td><?php echo $data['subject_name'];?></td>
<td><?php echo $data['marks'];?></td>
<td><?php echo $data['total'];?></td>
<?php
}
</tr>
</table>

Related

Retrieve User data from user id

I have just started learning PHP, so please bear with me. I have two tables in my database, users and appointments tables. The appointments table has idUsers as a foreign key for the id field in users table. I'm trying to display data from appointments table based on current user logged in. The problem is, it displays data from all users and not the one who logged in.
I'm using fetch_assoc() to get the data from the database.
<?php
while($row = $rows->fetch_assoc()): ?>
<td><?php echo $row['idApp'] ?></td>
<td class="col-md-10"><?php echo $row['fname'] ?> </td>
<td class="col-md-10"><?php echo $row['lname'] ?> </td>
<td class="col-md-10"><?php echo $row['email'] ?> </td>
<td class="col-md-10"><?php echo $row['phone'] ?> </td>
<td class="col-md-10"><?php echo $row['date'] ?> </td>
<td class="col-md-10"><?php echo $row['time'] ?> </td>
<td class="col-md-10"><?php echo $row['message'] ?> </td>
</tr>
<?php endwhile; ?>
The expected results would be for the user to log into the system and only see the data , that they have stored against their id.
you should to make "one to one relationship". for example you have two tables users and posts. the posts table must be some like: id,user_id,title,...
so when you want to get posts articles by specific user run this query:
$query = 'SELECT * FROM `posts` WHERE `user_id`= '.$_GET['id'].'';
however do not foget SQL injection!
learn PDO :)

How to write Orderby Clause with where with where clause to sort data

hi i'm trying sort mysql data in table but my problem is that my query is sorting all the data my table looks like
Word | Meaning | Synonym | Antonym
definitely | without doubt | certainly |possibly
great | of an extent, amount | considerable |little
zeal | great energy | passion | indiference
zealot | a person who is fanatical|fanatic | moderate
zealous | having or showing zeal. | fervent | apathetic
so when i search lets say word starting wit z then i get
Word | Meaning | Synonym | Antonym
zeal | great energy | passion | indiference
zealot | a person who is fanatical|fanatic | moderate
zealous | having or showing zeal. | fervent | apathetic
now i want to perform sorting on these searched data but my sort query is sorting the all data
my store procedure looks like
CREATE DEFINER=`root`#`localhost` PROCEDURE `SortContent`()
BEGIN
SELECT * from dictionarysearch ORDER BY word ASC;
END
and my php code is like
<?php
if(isset($_GET['search_btn'])){
$search=$_GET['search'];
$result=GetWords(mysqli_escape_string($conn,$search));
}
/*if(isset($_GET['q'])){
$id=$_GET['q'];
$result=GetWordsById($id);
}*/
if(isset($_GET['sort'])){
$sort=$_GET['sort'];
}
if(isset($_GET['sort'])){
if($sort=="asc"){
$result=SortContent();//Here i'm calling a function which is calling the store procedure
}
if($sort=="desc"){
$result=SortContent2();
}
}
else{
$result=GetAdminWords();
}
if(mysqli_num_rows($result)>0)
?>
<thead>
<tr>
<th>Word</th>
<th>Meaning</th>
<th>Synonym</th>
<th>Antonym</th>
</tr>
</thead>
<?php
while($row=mysqli_fetch_array($result)){
?>
<tbody>
<tr>
<td><?php echo $row['word'];?></td>
<td><?php echo $row['meaning'];?></td>
<td><?php echo $row['synonym'];?></td>
<td><?php echo $row['antonym'];?></td>
<td><i class="fa fa-edit"></i> <a onClick="javascript: return confirm('Please confirm deletion');" href="view.php?id=<?php echo $row['id'];?>"><i class="fa fa-trash"></i></a> </td>
</tr>
</tbody>
<?php
}?>
</table>
so i want to know how i can write a query so that it sorts only selected data and i have set id autoincrement
There is no need to use a stored Procedure for this. I suggest that the following code will work for you...
<?php
if(isset($_GET['search_btn']) && strlen($_GET['search'])){
$search = mysqli_escape_string($conn,$_GET['search']);
$sql = "SELECT * FROM dummydata WHERE info LIKE '".$search."%'";
if(isset($_GET['sort']) && in_array($_GET['sort'], array('ASC', 'DESC'))){
$sort=$_GET['sort'];
$sql .= " ORDER BY info ".$sort;
}
//echo "<h3>".$sql."</h3>";
if (($result=mysqli_query($conn,$sql))!==false) {
?>
<thead>
<tr>
<th>Word</th>
<th>Meaning</th>
<th>Synonym</th>
<th>Antonym</th>
</tr>
</thead>
<tbody>
<?php
while ($row=mysqli_fetch_assoc($result)){
//echo "<pre>".var_export($row,true)."</pre>";
?>
<tr>
<td><?php echo $row['word'];?></td>
<td><?php echo $row['meaning'];?></td>
<td><?php echo $row['synonym'];?></td>
<td><?php echo $row['antonym'];?></td>
<td>Your action url here.... </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
} else {
echo "<h3>Problem with SQL</h3>";
}
}
?>
I hope that this helps.

multiple id in sql how to expload and get the id values in codeigniter

Multiple id in mysql table how to expload and get the particular id values in another mysql table for codeigniter,
Pages view images added,
View page
`<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td><?=$loop->departments_list_id ?></td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>`
Controller Page
`public function company_settings() {
$data['company_branch'] = $this->settings_model->company_branch();
$this->load->view('settings/company_settings',$data);`
Model Page
`function company_branch(){
$this->db->select('company_branch.*,
company_departments.department_name as departments_name ')-
>from('company_branch');
$this->db- >join('company_departments','
company_branch.departments_list_id = company_departments.id');
$query = $this->db->get();
return $query->result();
}`
How to display the `departments_list_id ` to department names in view
page,
The image is company_branch and company_department mysql table view,
browser View page,
View Page
`<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td>
<?php
foreach(explode(",",$loop->departments_list_id) as $department)
{ $depart=$this->db->query("select department_name from company_departments where id=".$department)->row()->department_name;
echo $depart.',<br />';
}
?>
</td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>`
You can't enter some id into 1 field. You can add some fields to another id. Example field: departement_id_1, departement_id_2, departement_id_4, departement_id_5 etc depending on need.
you can change your sql select to this :
select branc.*,
(select dept.departement_name as departement_name_1 from company_departement as dept where branc.departement_id_1=dept.id),
(select dept.departement_name as departement_name_2 from company_departement as dept where branc.departement_id_2=dept.id),
(select dept.departement_name as departement_name_3 from company_departement as dept where branc.departement_id_2=dept.id)
from company_branc as b
then you can use script from your form
<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td><?=$loop->departments_name_1 ?></br>
<?=$loop->departments_name_2 ?></br>
<?=$loop->departments_name_3 ?>
</td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>
i hope this helps

Create new table based on php array field

I have an array of fields that came from a DB query.
I would like to find the best solution to create multiple tables based on unique values from a field $office_id.
So basically, I have tried using a foreach loop, store the office_id and check each time around the loop if it is unique or not, then close the last table html, start a new table html etc.
I hate this solution because it means the array must be ordered by office_id and it is quite ugly.
Can anyone suggest a better way to do this using cleaner, smarter code. Do I need to do some sort of transformation on the array? Is there a better way to pull out the unique office_id field?
I also want to use the office_id as a header for each table, so that the output looks similar to the following:
office_id One
-----------------------------------
settle | price | Lister | date
-----------------------------------
| | |
office_id Two
-----------------------------------
settle | price | Lister | date
-----------------------------------
| | |
This is what I have (ugly), and as I type, I realise I'm missing the close tag:
<?php $thisOffice = ""; ?>
<?php foreach ($fees as $fee): ?>
<?php if($thisOffice !== $fee->office_id) : ?>
<table class="table table-striped table-condensed">
<tr>
<th>Settlement</th>
<th>Office</th>
<th>Price</th>
<th>Lister</th>
<th>Sold</th>
</tr>
<?php endif; ?>
<tr>
<td> <?= $fee->settle_date ?></td>
<td> <?= $fee->office_id ?></td>
<td> <?= $fee->price ?></td>
<td> <?= $fee->lister ?></td>
<td> <?= $fee->sale_date ?></td>
</tr>
<?php $thisOffice = $fee->office_id; ?>
<?php endforeach; ?>
It will make your life much easier if you split the array into three dimensions:
$offices[$id][] = $fee;
This way you can use:
foreach ($offices as $id => $office) {
foreach ($office as $fee) {
// Build table
}
}

how to subtract two columns output in a third column using PHP

Using PHP I want to subtract each values of used column from credit column. I have created a tableA in phpmyadmin.
remaining:credit-used I can not get the results i just get same values of credit.
// The desire output in table A
id |date |credit |used|remaining|
1 |20-5-2013 |400 |300 |100 |
2 |19-5-2013 |300 |100 |200 |
3 |18-5-2013 |600 |50 |550 |
// db connection
query select all from tableA
$sql = "SELECT * FROM tableA";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
?>
fetch the information and output into a table:
<table >
<tr>
bla bla..
</tr>
// echo:
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["date"]; ?></td>
<td><?php echo $row["credit"]; ?></td>
<td><?php echo $row["used"]; ?></td>
I'm trying to do this query to get remaining:
// <td> <?php> echo $total= $row["credit"] - $row["used"];?>
</td>
</tr>
</table>
can anyone give me a hints or highlight the error. Than you very much in advance.
What you have should work, but why not let MySQL do the calculation?
mysql_query("SELECT *, `credit`-`used` AS `remaining` FROM `tableA`");
You have a closing bracket on your php
<td> <?php echo $row["credit"] - $row["used"]; ?> </td>

Categories