Hi everyone i am working on a small beginner project currently i am working on displaying two separate table data on a web page in HTML table but i am having some problems with the data. There is a credentials table and an answers table a user first enters its credentials and then presses the button take survay which stores the credentials on to a table and redirects the user to a question page where he picks a multiple choice question and the users choices are saved to a table. all this data then is displayed on web page but when the data is displayed the answers get duplicated fore every user that enter its details. can someone tell me where have i gone wrong ? tnx for your help in advance.
view
<table border="1">
<tr>
<th>Name</th>
<th>Second Name</th>
<th>Phone</th>
<th>Email</th>
<th>Answer</th>
<th>Comment</th>
</tr>
<?php foreach ($query as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->second_name; ?></td>
<td><?php echo $row->phone; ?></td>
<td><?php echo $row->email; ?></td>
<td> <?php echo $row->answerA;?>
<?php echo $row->answerB;?>
<?php echo $row->answerC;?></td>
<td><?php echo $row->comment;?><br></td>
</tr>
<?php endforeach; ?>
</table>
controller
function getall(){
$this->load->model('result_model');
$data['query'] = $this->result_model->result_getall();
$this->load->view('result_view', $data);
}
model
function result_getall(){
return $this->db->select('tblanswers.*,credentials.*')
->from('tblanswers, credentials')
->get()
->result_object();
}
You are selecting everything from tblanswers, credentials without joining on any column. You're ending up with a cross join, which pairs each row with every row from the other table. You need to specify which rows from tblanswers relate to which rows from credentials. E.g. if there is an id column in credentials then you need to create a credentials_id column in tblanswers and enter the data so that they match. Your getall() should look something like this:
function result_getall(){
return $this->db->select('tblanswers.*,credentials.*')
->from('tblanswers')
->join('credentials', 'tblanswers.credentials_id = credentials.id')
->get()
->result_object();
}
The line which says
join('credentials', 'tblanswers.credentials_id = credentials.id')
specifies how the two tables relate.
Suppose Mr Jones enters his credentials and they are saved in a row in the credentials table with id 72. Then he enters his answers. You need to make sure that when you save them, you enter "72" in the credentials_id column of the answers table. That signifies that these answers belong to Mr Jones. When you run the query with the join specified above, it will return Mr Jones' credentials against Mr Jones' answers.
Related
Im a begginer in the whole SO and programming enviroment, and here comes my first question(which i tried finding in here but with no luck so far..)
I have an html table that prints specific patients of the doctor that is active (in a simple web-application that i created). The doctor gets filtered using $_SESSIONS global var in php.
My problem is that i want to implement a few actions in the same HTML table that displays the patients, like view history (which is stored in a local DB table, from an HTML from using POST method) and create a new form for the same person.
I saw that providing the table with row.ids could be a solution, but my table isn't static, i woulda like for the user to have the option to add/delete patients..
Following is a sample of my code that displays the HTML table "Existing Patients" :
<table>
<tr>
<th>Patient Id</th><th>Patient Name</th><th>Phone Number</th><th>Email</th><th>History</th>
<th>Add a Follow Up Visit</th><th>Remove Patient</th>
</tr>
<?php $sql = "SELECT * FROM patients WHERE Doctor_ID = $usersid";
$result = $pdo->query($sql);
if($result->rowCount() > 0){
while($row = $result->fetch()){?>
<tr>
<td><?php echo $row['Patient_id']; ?></td>
<td><?php echo $row['Patient_name'] ; ?></td>
<td><?php echo $row['Phonenum'] ; ?></td>
<td><?php echo $row['Email']; ?></td>
<td><?php echo "<a href='/patienthistory.php'</a>" . 'Previous Visits'; ?></td> //problem here
<td><?php echo "<a href='/patientform.html'</a>" . 'Add Follow Up' ; }?></td> //and here
</tr>
</table>
I want the last 2 lines to connect immediately to the specific database stored Patient_id and retrieve the existing information stored there from the patients previous visits.
I hope i gave enough of a description, but if there is any more info neccesary, please let me know.
Thank you in advance!
You can specify the patient ID as a query parameter in the URL that you're linking to. Also the HTML you're generating for your links is invalid currently. And the second link is going to need to go to a .php script, not a .html file, if you want it to execute code and fetch data.
Try this example:
<td><?php echo "<a href='/patienthistory.php?id='".$row['Patient_id']."'>Previous Visits</a>"; ?></td>
<td><?php echo "<a href='/patientform.php?id='".$row['Patient_id']."'>Add Follow up</a>"; ?></td>
Then in each of the PHP scripts, use $_GET["id"] to retrieve that ID of the patient and use that in a query
e.g.
$patientID = $_GET["id"];
I hope the title makes sense and sorry if this is a duplicate I wasn't certain what to search for to find the correct answer.
I have a table on a website that pulls info from a database. So my issue is I have two tables in the database, the_traveler and the_discipline. What I am doing is pulling the value from the traveler that looks like this 89,43 and after I LEFT JOIN with the_disciplines table, to get the name that correlates with the numbers, like so:
$query ="SELECT t.name,t.traveler_id, t.disciplines, dis.name as discname
FROM ".TABLEPRIFIX."traveler t
LEFT JOIN ".TABLEPRIFIX."discipline dis ON t.disciplines = dis.id";
$disname = $obj->queryResult($query);
I am only able to echo out the first numbers name by doing this:
<?php
foreach($disname as $traveler)
{
<tr>
<td><?php echo $traveler['discname']; ?></td>
</tr>
<?php
}
?>
My question is how would I change my code to get all the numbers from the database to echo in each row. I have tried to implode the numbers.
If you want to get a single row with all values you must change the code to:
<tr>
<?php
foreach($disname as $traveler)
{
<td><?php echo $traveler['discname']; ?></td>
<?php
}
?>
</tr>
as i am a beginner so i want to ask how can i run the multiple queries using PHP for the same row to have multiple data in the table in html. i have tried many attempts but nothing worked i am using MYSQL as backend. I know that i have not written code for the first row and second column i.e. the second <td> on wicket keeper but the thing is that i want all rounder data to come in the second <td> of wicket keeper section. when running this code it is providing the output fine for wicket keeper but for all rounder column the data is starting after the end of wicket keeper data. i know why this problem is occurred but how can i solve this please tell??
CODE HERE:
<?php
$sql="select * from teams where role = 'WicketKeeper'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row["name"] ?></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
<?php
$sql="select * from teams where role = 'AllRounder'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
?>
<tr>
<td></td>
<td><?php echo $row["name"] ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
Here is a good way to generate the rows of the display you showed.
First, use this query:
SELECT name, role FROM teams ORDER BY role DESC;
It generates rows in the order you want them, wicketkeepers first.
Then, use this php code to generate your display.
<?php
$sql="SELECT name, role FROM teams ORDER BY role DESC";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
$role = $row["role"];
$name = $row["name"];
$wicketkeeper = $role == "WicketKeeper" ? $name : "";
$allrounder = $role == "AllRounder" ? $name : "";
?>
<tr>
<td><?php echo $wicketkeeper ?></td>
<td><?php echo $allrounder ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
This renders the rows in the result set from your query, placing names in columns according to their roles.
In the general case, this task is known as pivoting a table.
Pro tip 1 Try to avoid SELECT * in queries. Instead give the names of the columns you need. This makes your code easier for a stranger to read, and it may make your queries run faster.
Pro tip 2 Anytime you catch yourself reusing the same query where it only varies by a WHERE clause, try to use a single query. This can make your application run faster.
I'm kinda stuck here! I have to two tables free_members and domstic_members and I need to move selected rows from free_members tables to the domestic_members table. why I'm saying selected rows because of I at the end of every row in free_members tables there is an upgrade option which will move that row to the domestic_members table. I have the code but it's not working the way I want. right now what is happening is when I click on upgrade option it just copies the whole table records and sends it's to the other table. this here is the code
Controller
function upgradeprofile() {
// $this->load->database();
$this->load->model('freemember_model');
$this->freemember_model->upgrade();
}
model
function upgrade() {
$query = $this->db->get('free_members');
foreach ($query->result() as $row) {
$this->db->where('id', $member_id);
$this->db->insert('domestic_members', $row);
}
}
View
<center> Search Member:
<input type="text" name ='search' id='search' placeholder="Search a member"/></center>
<input type="hidden" name ='limit_from' id='limit_from'/>
</form>
<br><center><button type="button" onclick="CallPrint1('background12')">Print</button>
<div id="background12" class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Sr#</th>
<th><center>Full Name</th>
<th><center>Mobile Number</th>
<th><center>Membership# / CNIC</th>
<th><center>Email Address</th>
<th><center>Province</th>
<th><center>District</th>
<th><center>Tehsil</th>
<th><center>Union Council</th>
<?php /*?> <?php if($sessiondata['deletemembers']):?><?php */?>
<?php if($sessiondata['username'] != 'bilal.sabir'):?>
<th><center>Delete</th>
<?php endif;?>
<?php /*?><?php if($sessiondata['data_modification'] && $sessiondata['username'] != 'bilal.sabir' ):?><?php */?>
<?php if($sessiondata['username'] != 'bilal.sabir'):?>
<th><center>Print</th>
<th><center>Edit</th>
<th><center>Change Pwd</th>
<th><center>Upgrade</th>
<?php endif;?>
</tr>
</thead>
<tbody>
<?php $sr=0;?>
<?php foreach ($freemembers as $member): ?>
<tr><td><center><?php echo $sr+=1; ?></td><td><center><?php echo $member->first_name; ?></td>
<td><center><?php echo $member->mobile; ?></td><td><center><?php echo $member->cnic; ?></td>
<td><center><?php echo $member->email; ?></td>
<td><center><?php echo $member->province; ?></td><td><center><?php echo $member->district; ?></td><td><center><?php echo $member->tehsil; ?></td><td><center><?php echo $member->uc; ?></td>
<?php /*?> <?php if($sessiondata['deletemembers']):?><?php */?>
<?php if($sessiondata['username'] != 'bilal.sabir'):?>
<td><center>delete</td>
<?php endif;?>
<?php /*?><?php if($sessiondata['data_modification'] && $sessiondata['username'] != 'bilal.sabir' ):?><?php */?>
<?php if($sessiondata['username'] != 'bilal.sabir'):?>
<td><center>print</td>
<td><center>edit</td>
<td><center>change pwd</td>
<td><center>Upgrade</td>
<?php endif;?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
this is the view of my table free_members
Based upon your View, you have this...
index.php/admin/upgradeprofile/" . $member->id
So if $member->id = 100 the url will be
index.php/admin/upgradeprofile/100
So you are passing a member_id as mentioned in your model code BUT you are not getting it nor are you using the where correctly.
So you need to get a handle on the member_id...
This is ONE Solution/idea...This relies on you providing a correctly validated integer... You need to always Check/validate your inputs...
Controller
function upgradeprofile($member_id) {
// Need to check that $member_id is an integer
// To be added by the OP
$this->load->model('freemember_model');
// $member_id is passed in via the URL.
$this->freemember_model->upgrade($member_id);
}
Model
function upgrade() {
// Which member do we want to get from the free_members Table?
$this->db->where('id', $member_id);
$query = $this->db->get('free_members');
// Did we get a result? i.e was a valid member id passed in?
if($query !== false) {
$row = $query->row();
$this->db->insert('domestic_members', $row);
} else {
// Do nothing or handle the case where an illegal number was used...
}
}
Note: This code is not tested and is only to be used as a guide.
It can be refactored a number of ways but I am trying to keep it simple for now.
So please, do not just copy and paste this without at least "thinking" about what you are trying to do and how the code might be written to achieve it.
The main points here are...
1. You have to use a member id of some kind so you can retrieve the record for the free member so you can insert it into the other table.
2. You are providing this "member id" in the url so you need to extract it.
3. You can do that by either setting it as a parameter in the method being called... Or you can use segments ( look it up ).
So once you have the member id you can fetch the free member entry and insert it into the other table BUT What do you want to happen to the entry in the free members table because that is still going to be there and now you have two copies of the same information... Delete it?
If you need further explanation - ask.
One last point.
You should be making sure you fully understand what is you are doing and not just doing it because someone said to or you thought it was a good idea... Have a Good Reason and think of the implications...
I'm not familiar with Codeigniter but MySQL syntax should be this:
INSERT INTO table1 (table1.row1, table1.row2)
SELECT table2.row1, table2.row2
FROM table2
WHERE PUT_YOUR_CONDITION_IF_NEEDED
I've found this for Codeigniter.
I am trying to make a dynamic HTML table with PHP, populating it with data from MySQL database. So far I have tried the while loop, but the result ends up in displaying the same first row it gets multiple times.
<div class = "container">
<p>Registered companies:</p>
<table border = "1px" align = "left">
<tr>
<th>Username</th>
<th>Company name</th>
<th>Company value1</th>
<th>Company value2</th>
</tr>
<?php
$compRowIncrement = 0;
while ($compRowIncrement < $companyRowCount) {
?>
<tr>
<td><?php echo $companyRow['user_name']?></td>
<td><?php echo $companyRow['company_name']?></td>
<td><?php echo $companyRow['company_value1']?></td>
<td><?php echo $companyRow['company_value2']?></td>
</tr>
<?php
$compRowIncrement++;
}
?>
</table>
</div>
It should display 3 rows of data for example (SQL query returns 3 different values). But so far I have achieved to get 3 rows (like I need) with the same data (first value it gets from the database).
How do I do it so each table row is populated with different data, as it is in the database.
I'm just learning, so if you don't mind ignore the css values in table :).
EDIT1 (Added query)//
$getPlayerCompanies = $MySQLi_CON -> query("SELECT DISTINCT *
FROM companies
LEFT JOIN player ON companies.player_id = player.player_id
LEFT JOIN users ON users.user_id = player.user_id
WHERE users.user_id =".$_SESSION['userSession']);
$companyRow = $getPlayerCompanies -> fetch_array();
$companyRowCount = $getPlayerCompanies -> num_rows;
Following query currently returns 3 rows, like it should.
where did the $companyRow values get populated?
I belive your code should be more like this (or with mysqli commands)
<?php
while ($companyRow = $getPlayerCompanies -> fetch_array() )
{
?>
<tr>
<td><?php echo $companyRow['user_name']?></td>
...
</tr>
<?php
}
?>