So, I have an sql table that doesn't have a unique column. I have to display it in a table by it's ID. But, like I said, the ID is not unique
OBS¹: Notice that it comes from a select statement
OBS²: the user id can appear in the select from 0 up to 4 times (according to id_questionario)
So, the thing is, how am I going to make a table that will show only one time user_id but will display by it's side all the melhor_tentativa according to id_questionario (notice that id_questionario is not to be displayed, only so I can display the melhor_tentativa in the correct order)
What I understand is that I have lines that should be columns. I've been trying for 2 days to figure this out and turns out I can't. Thought that it might be miss in how I made my sql
This is how the table looks
This is how the sql table looks
And bellow is the code I made for it
<?php
$count = 0;
$nota1 = 0;
$nota2 = 0;
$nota3 = 0;
$nota4 = 0;
?>
<?php
foreach($notas as $nota):
$total = 0;
?>
<tr>
<td><?php echo $nota['user_id']?></td>
<td>NULL</td>
<?php
if($nota['id_questionario']==1){
$nota1 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota1 ?></td>
<?php
if($nota['id_questionario']==2){
$nota2 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota2 ?></td>
<?php
if($nota['id_questionario']==3){
$nota3 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota3 ?></td>
<?php
if($nota['id_questionario']==4){
$nota4 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota4 ?></td>
<td><?php $total = ($nota1 + $nota2 + $nota3 + $nota4)/4; echo $total; ?></td>
</tr>
<?php $count++; ?>
<?php
endforeach;
echo $count;
?>
</table>
P.S: I know that total is completly wrong ;)
After a bunch of work I figured this out. So what I did was the following. I had that table which would show 4 results for a one ID. I made a view out of this table
Then I did a subquery from that view, so I would be able to show all results in one line
Hope it helps anybody in the future :)
Related
I have a table like this.
author_id author_state author_name
1 CA Hello
2 MI World
3 CA How
4 MI Are
Please let me know how can I achieve this. In other words, I should loop it and combine the state values first and display the details.
Here is my PHP Code
<table>
<tr><td>Author ID</td><td>Author State</td><td>Author Name</td></tr>
<?php
$row_data = mysql_query("select * from author" );
while($row = mysql_fetch_array($row_data) ) {
?>
<tr>
<td><?php echo $row['author_id']; ?></td>
<td><?php echo $row['author_state']; ?></td>
<td><?php echo $row['author_name']; ?></td>
</tr>
<?php } ?>
</table>
My Expected output is to combine the state and display something like below:
Author ID Author Name
=======================================
CA
==
1 Hello
3 How
MI
==
2 World
4 Are
You can do this fairly easily, by sorting your output by author_state, and using a variable to keep track of which state you're outputting. If the state changes, then you simply output the state as a new header.
You can take advantage of the SQL order by clause so that you don't have to sort the data yourself.
Here is an edit of your code, where I've simply added the order by to your mysql_query(), and added a variable for $current_state.
<table>
<tr><td>Author ID</td><td>Author State</td><td>Author Name</td></tr>
<?php
$current_state = "";
$row_data = mysql_query("select * from author order by author_state" );
while($row = mysql_fetch_array($row_data) ) {
// Output the "new" state
if ($row['author_state'] != $current_state)
{
echo "<tr><td colspan=\"3\">{$row['author_state'}}</td></tr>";
$current_state = $row['author_state'];
}
?>
<tr>
<td><?php echo $row['author_id']; ?></td>
<td><?php echo $row['author_state']; ?></td>
<td><?php echo $row['author_name']; ?></td>
</tr>
<?php } ?>
</table>
As a side note, I would strongly encourage you to look into using mysqli or PDO instead of the mysql_* functions. The mysql_* functions are only availble in very old versions of PHP (they were completely removed in PHP7).
I currently have a table on my website which I have in my SQL database and I need each row of my table to get certain information from the columns. I have something that works just fine at the moment, however, I have a feeling its not the best way of doing things. As I have around 600+ rows in my database.
I'm trying to learn more about all of this so if you have any idea to do this in a better way then that would be fantastic.
<?php
$queryContent="SELECT * FROM businesses WHERE id= 104";
$resultContent= $mysqli->query($queryContent);
$rowContent = $resultContent->fetch_assoc();
?>
<?php if ($rowContent['active'] == "Y") { ?>
<tr>
<td><?php echo $rowContent['businessname']; ?></td>
<td><?php echo $rowContent['telephoneno']; ?></td>
<td><?php echo $rowContent['emailaddress']; ?></td>
<td><?php echo $rowContent['rating']; ?></td>
</tr>
<?php } ?>
If you are trying to list all active rows, try something like this:
<?php
// Change query to select only acitve rows
$queryContent="SELECT * FROM businesses WHERE active = 'Y'";
$resultContent= $mysqli->query($queryContent);
//while cycle will repeat while sql have results
while($rowContent = $resultContent->fetch_assoc()){
?>
<tr>
<td><?php echo $rowContent['businessname']; ?></td>
<td><?php echo $rowContent['telephoneno']; ?></td>
<td><?php echo $rowContent['emailaddress']; ?></td>
<td><?php echo $rowContent['rating']; ?></td>
</tr><?php } ?>
Php documentation examples should be also usefull.
When you have a list of IDs wich you want to select from sql look at SQL WHERE - IN operator and syntax.
SELECT * FROM businesses WHERE id IN (103, 104, 105)
I could think of two things which can be improved in your code:
instead of select *, you can mention select column1, column2,... which is considered to be the standard
And if you are trying to list only one row, then either access the $rowcontent array with index 0 (or) limit the select query's output rows by using "LIMIT 1" at the end of "SELECT" query (second option valid only if you are not sure about the query whether it will fetch only one row always or not)
I am a little bit confused about using while loop inside another. Everything is fine with the second loop but the first one only returns one value and it is the same.
$aukciono_laimetojai_while = mysql_query("SELECT id, user_id, date, win
FROM tb_auction_winners WHERE user_id = 206");
$aukciono_istorija_while = mysql_query("SELECT user_id, aukciono_id,
COALESCE(SUM(bid), 0) AS bid, date FROM tb_aukciono_istorija
WHERE user_id = 206 GROUP BY aukciono_id");
while ($r1 = mysql_fetch_assoc($aukciono_istorija_while)) {
while ($r2 = mysql_fetch_assoc($aukciono_laimetojai_while)) { ?>
<tr>
<td><?php echo $r2['date']; ?></td>
<td><?php echo $r2['win'] - $r1['bid']; ?> Eur</td>
<td>0 Eur</td>
<td>Plačiau <?php echo $r2['win'] . ' - ' . $r1['bid']; ?></td>
</tr>
<?php } } ?>
$aukciono_laimetojai_while returns:
click here
$aukciono_istorija_while returns: click here
Using these 2 while loops, the table looks like this: (www.i.stack.imgur.com/Os8Rx.png) (can't use more than 2 links, sorry)
Something is wrong with the second number (0.14 should not be the same in every row, only the first one) and it should return 3 rows ($r1 = mysql_fetch_assoc($aukciono_istorija_while) has 3 rows in the database). I do not know what is wrong here, using one while loop, everything is just fine. Could someone help me, please?
I have found the solution. I should use only one query in the while loop. Instead of this block of code:
$aukciono_laimetojai_while = mysql_query("SELECT id, user_id, date, win
FROM tb_auction_winners WHERE user_id = 206");
$aukciono_istorija_while = mysql_query("SELECT user_id, aukciono_id,
COALESCE(SUM(bid), 0) AS bid, date FROM tb_aukciono_istorija
WHERE user_id = 206 GROUP BY aukciono_id");
while ($r1 = mysql_fetch_assoc($aukciono_istorija_while)) {
while ($r2 = mysql_fetch_assoc($aukciono_laimetojai_while)) { ?>
<tr>
<td><?php echo $r2['date']; ?></td>
<td><?php echo $r2['win'] - $r1['bid']; ?> Eur</td>
<td>0 Eur</td>
<td>Plačiau <?php echo $r2['win'] . ' - ' . $r1['bid']; ?></td>
</tr>
<?php } } ?>
I should use this (1 query, 1 while loop):
$aukciono_laimetojai_ir_aukciono_istorija_while = mysql_query("
SELECT tai.aukciono_id tai_aukciono_id, taw.win taw_win, taw.date taw_date,
COALESCE(SUM(tai.bid), 0) tai_bid FROM tb_auction_winners taw
JOIN tb_aukciono_istorija tai ON taw.id = tai.aukciono_id
WHERE tai.user_id = $usid GROUP BY aukciono_id");
while ($r1 = mysql_fetch_assoc($aukciono_laimetojai_ir_aukciono_istorija_while)) { ?>
<tr>
<td><?php echo $r1['taw_date']; ?></td>
<td><?php echo $r1['taw_win'] - $r1['tai_bid']; ?> Eur</td>
<td>0 Eur</td>
<td class="placiau" data-aukciono-id=
"<?php echo $r1['tai_aukciono_id']; ?>">Plačiau</td>
</tr>
<?php } ?>
I'm trying to display the date from the database using the following code.
<?php
$query_course = mysqli_query($databaseConnection,"SELECT * FROM grading WHERE courseid=".$courseid);
$count=0;
while($row = mysqli_fetch_array($query_course))
{
$gradename[$count]= $row['gradingname'];
$percentage[$count]= $row['percentage'];
$date[$count]= $row['date'];
?>
<tr>
<td><?php echo $gradename[$count]; ?></td>
<td><?php echo $percentage[$count]; ?></td>
<td><?php echo $date[$count]; ?></td>
</tr>
<?php $count++;
} ?>
But it displays only the first letter/digit for the date. Database looks fine. What might be the problem?
The only thing that would explain this is if $gradename etc. were initialised as strings. E.g.:
$foo = '';
$foo[0] = 'bar';
echo $foo[0]; // b
Make sure you initialise your variables as arrays before the loop:
$gradename = array();
...
(I'd question the usefulness of those extra arrays to begin with, but that's outside the scope of this question.)
it displays the first digit because you use the count as index. Since the count is 0 it display just the first digit from date. Remove the $count and echo the $row['date'].
So I have 3 tables I wish to join.
I am building an app i Codeigniter and I have 3 tables
Client:
-id
-phone_number
-hospital_id
-smc_status
-testing_center_id
Hospital
-id
-name
Testing_center
-id
-name
In the model,I have this:
public function get_clients()
{
if($slug === FALSE)
{
$this->db->select('clients.*');
$this->db->from('clients');
$this->db->join('hospital', 'clients.id = hospital.id');
$this->db->join('testing_center', 'clients.id = testing_center.id');
$query = $this->db->get();
return $query->result_array();
}
$query = $this->db->get_where('clients');
return $query->row_array();
}
In the view I have:
<tbody>
<?php foreach ($clients as $client_item): ?>
<tr>
<td><?php echo $client_item['phone_number'] ?></td>
<td><?php echo $client_item['smc_status'] ?></td>
<td><?php echo $client_item['hospital_id'] ?></td> //i wish to have the hospital name here
<td><?php echo $client_item['testing_center_id'] ?></td> //i wish to have the testing center name here
<td><?php echo $client_item['language'] ?></td>
<td>View</td>
</tr>
<?php endforeach ?>
</tbody>
But that is because I have failed to show the hospital name and the testing center name on the third and fourth td. How can I go about that? I tried a few techniques that just did not seem to work for some reason. Please advise
You're only selecting the values from the clients table. You need to select the columns from the other tables as well
$this->db->select('clients.id,
clients.phone_number,
clients.smc_status,
clients.language,
hospital.name AS hospital_name,
testing_center.name AS testing_center_name');
Then you can access them by
<?php echo $client_item['hospital_name'] ?>
<?php echo $client_item['testing_center_name'] ?>
EDIT: Also you shouldn't use SELECT *, which clients.* is doing. Updated my code.
What happens if you try this:
$this->db->join('hospital', 'hospital.id = clients.id');
$this->db->join('testing_center', 'testing_center.id = clients.id');
instead of this:
$this->db->join('hospital', 'clients.id = hospital.id');
$this->db->join('testing_center', 'clients.id = testing_center.id');
also check
client*s* and client if they are the same everywhere
And also change as Nerd proposed:
$this->db->select('clients.*');
to:
$this->db->select('*');
It sholud be like this
$this->db->join('hospital', 'clients.hospital_id = hospital.id');
$this->db->join('testing_center', 'clients.testing_center_id = testing_center.id');