i have this on my database table
id_a | nama | email |
1 | philipe | philipe#mail.com |
2 | dora | dora#mail.com |
3 | John | john#mail.com |
this code on my view
<tr>
<td><label for="cities">Select attendances</label></td>
<td> : </td>
<td> <select class="multiselect" multiple="multiple" name="id_c[]" title="click to select Attendances">
<?php foreach ($test as $data): ?>
<option value="<?php echo $data->id_a ?>" ><?php echo $data->nama ?></option>
<?php endforeach; ?>
</select></td></tr>
in my code i have option value from my database = 1 2 3
what i want to do is send email, with an email address from my database
$id_c = $this->input->post('id_c');
foreach($id_c as $id_a)
{
$query="SELECT email From mstr_attend WHERE id_a = ".$id_a.""; }
and here's the problem
what i get from this code is philipe#mail.comdora#mail.comjohn#mail.com
what i need is philipe#mail.com, dora#mail.com, john#mail.com
a comma after one email
can someone,give me some solution so i can get that
Assuming you want a string, you could loop into an array and then implode.
$emails = array();
foreach( $ids as $i )
{
//do query here, then store in the emails array
$emails[] = $query_output_email;
}
$comma_separated = implode( ', ', $emails ); //should output as email#dot.com, email#dot.com
You can get the stated result using the following code:
$id_c = $this->input->post('id_c');
$all_id_c = implode(',', $id_c);
$this->db->select('group_concat(email) as emails');
$this->db->where('id_a IN ('.$all_id_c.')');
$query = $this->db->get('mstr_attend');
$result = $query->row_array();
echo $result['emails']; // here is your result.
This link will surely help you to generate the query result.
Related
I want a loop, that echoes some football matches and my users bets on them, and then the possibillity to change the bets.
I have two tables in my db:
vm_kampe which is the matches where res is the result:
|id |hhold |uhold |res |
|1 |Rusland |Saudi |NULL |
|2 |Egypten |Uruguay |NULL |
... and so on
and vm_kampe, which is the users bets:
|id |resu |
|1 |2-1 |
|2 |1-3 |
... and so on.
My update site is like this:
SELECT * FROM vm_kampe k JOIN vm_207 u ON k.id = u.id
...
foreach($results as $row){
echo '<div class="vm_kupon_row">
<div class="vm_id"><input type="hidden" name="kampids[]" value="'.$row->id.'"></div>
<div class="vm_kampe">'.$row->hhold.' - '.$row->uhold.'</div>
<div class="vm_result"><input class="vm_resultat" type="text" name="resultats['.$row->id.']" placeholder="X-X" value="'.$row->resu.'"></div>
<input type="submit" class="godkend-vm-kupon" name="submit['.$row->id.']" value="Godkend">
My POST site is like this:
$res = $mysqli->real_escape_string($_POST['resultats']);
$id = $mysqli->real_escape_string($_POST['kampids']);
FOREACH ($_POST as $p) {
$gid = $p['id'];
$result = $p['res'];
$sql = "UPDATE vm_207 SET resu = '$result' WHERE id = '$gid'";
};
But if I try editting a bet and press submit, the bet will just stay the same as before. All I want is for me to be able to update every match. Please help :)
UPDATE
I just tried this in the post code:
$id = $_POST['kampids'];
$res = $_POST['resultats'];
foreach ( $id as $key => $k) {
print "The match is " .$k. " and the result is " .$res[$key];
};
I tried writing 2-1 in the first game and the print was:
The match is 1 and the result is 2-1
The match is...
So as I can see, the function works, so now I just need it to update the table..
In your hidden field, you are not setting the index number statically like you are with the other inputs.
<div class="vm_id"><input type="hidden" name="kampids['.$row->id.']" value="'.$row->id.'"></div>
I think this question / answer should help.
How to get form input array into PHP array
I am a newbie to PHP and I am stuck at a certain point. I tried looking up a solution for it however, I didn't find exactly what I need.
My goal is to create a leaderboard, in which the values are displayed in descending order plus the rank and score are displayed. Furthermore, it should also display whether or not a tie is present.
The database should look like this:
+---------+------+----------------+-------+------+
| user_id | name | email | score | tied |
+---------+------+----------------+-------+------+
| 1 | SB | sb#gmail.com | 1 | 0 |
+---------+------+----------------+-------+------+
| 2 | AS | as#web.de | 2 | 0 |
+---------+------+----------------+-------+------+
| 3 | BR | br#yahoo.com | 5 | 1 |
+---------+------+----------------+-------+------+
| 4 | PJ | pj#gmail.com | 5 | 1 |
+---------+------+----------------+-------+------+
And the outputted table should look something like this:
+------+-------------+-------+------+
| rank | participant | score | tied |
+------+-------------+-------+------+
| 1 | BR | 5 | Yes |
+------+-------------+-------+------+
| 2 | PJ | 5 | Yes |
+------+-------------+-------+------+
| 3 | AS | 2 | No |
+------+-------------+-------+------+
| 4 | SB | 1 | No |
+------+-------------+-------+------+
I managed to display the rank, participant and the score in the right order. However, I can't bring the tied column to work in the way I want it to. It should change the value, whenever two rows (don't) have the same value.
The table is constructed by creating the <table> and the <thead> in usual html but the <tbody> is created by requiring a php file that creates the table content dynamically.
As one can see in the createTable code I tried to solve this problem by comparing the current row to the previous one. However, this approach only ended in me getting a syntax error. My thought on that would be that I cannot use a php variable in a SQL Query, moreover my knowledge doesn't exceed far enough to fix the problem myself. I didn't find a solution for that by researching as well.
My other concern with that approach would be that it doesn't check all values against all values. It only checks one to the previous one, so it doesn't compare the first one with the third one for example.
My question would be how I could accomplish the task with my approach or, if my approach was completely wrong, how I could come to a solution on another route.
index.php
<table class="table table-hover" id="test">
<thead>
<tr>
<th>Rank</th>
<th>Participant</th>
<th>Score</th>
<th>Tied</th>
</tr>
</thead>
<tbody>
<?php
require("./php/createTable.php");
?>
</tbody>
</table>
createTable.php
<?php
// Connection
$conn = new mysqli('localhost', 'root', '', 'ax');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL Query
$sql = "SELECT * FROM names ORDER BY score DESC";
$result = $conn->query("$sql");
// Initalizing of variables
$count = 1;
$previous = '';
while($row = mysqli_fetch_array($result)) {
$current = $row['score'];
$index = $result['user_id']
if ($current == $previous) {
$update = "UPDATE names SET tied=0 WHERE user_id=$index";
$conn->query($update);
}
$previous = $current;
?>
<tr>
<td>
<?php
echo $count;
$count++;
?>
</td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['score'];?></td>
<td>
<?php
if ($row['tied'] == 0) {
echo 'No';
} else{
echo 'Yes';
}
?>
</td>
</tr>
<?php
}
?>
I think the problem is here
$index = $result['user_id'];
it should be
$index = $row['user_id'];
after updating tied you should retrieve it again from database
So I solved my question by myself, by coming up with a different approach.
First of all I deleted this part:
$current = $row['score'];
$index = $result['user_id']
if ($current == $previous) {
$update = "UPDATE names SET tied=0 WHERE user_id=$index";
$conn->query($update);
}
$previous = $current;
and the previous variable.
My new approach saves the whole table in a new array, gets the duplicate values with the array_count_values() method, proceeds to get the keys with the array_keys() method and updates the database via a SQL Query.
This is the code for the changed part:
// SQL Query
$sql = "SELECT * FROM names ORDER BY score DESC";
$result = $conn->query("$sql");
$query = "SELECT * FROM names ORDER BY score DESC";
$sol = $conn->query("$query");
// initalizing of variables
$count = 1;
$data = array();
// inputs table into an array
while($rows = mysqli_fetch_array($sol)) {
$data[$rows['user_id']] = $rows['score'];
}
// -- Tied Column Sort --
// counts duplicates
$cnt_array = array_count_values($data);
// sets true (1) or false (0) in helper-array ($dup)
$dup = array();
foreach($cnt_array as $key=>$val){
if($val == 1){
$dup[$key] = 0;
}
else{
$dup[$key] = 1;
}
}
// gets keys of duplicates (array_keys()) and updates database accordingly ($update query)
foreach($dup as $key => $val){
if ($val == 1) {
$temp = array_keys($data, $key);
foreach($temp as $k => $v){
$update = "UPDATE names SET tied=1 WHERE user_id=$v";
$conn->query($update);
}
} else{
$temp = array_keys($data, $k);
foreach($temp as $k => $v){
$update = "UPDATE names SET tied=0 WHERE user_id=$v";
$conn->query($update);
}
}
}
Thank you all for answering and helping me get to the solution.
instead of the update code you've got use something simular
$query = "select score, count(*) as c from names group by score having c > 1";
then you will have the scores which have a tie, update the records with these scores and your done. Make sure to set tie to 0 at first for all rows and then run this solution
UPDATE for an even faster solution sql based:
First reset the database:
$update = "UPDATE names SET tied=0";
$conn->query($update);
All records have a tied = 0 value now. Next update all the records which have a tie
$update = "update docs set tied = 1 where score IN (
select score from docs
group by score having count(*) > 1)";
$conn->query($update);
All records with a tie now have tied = 1 as we select all scores which have two or more records and update all the records with those scores.
my Database table field look like this
id - name - school - subjects - marks
1 - Adam - Highschool - Geography,physics,math - 60,50,40
2- - Jhone - elementry - Math,Language,Geography - 90,20,10
the php file should convert this values into html table using boot strap
my expected reslut to be like this
Hello Jone! this is your results
Math : 90
Language : 20
Geography : 10
I tried different methods like convert the result into array but it did work with me will.
one of them are
<?php
$shop = array( array("title"=>"rose", "price"=>1.25 , "number"=>15),
array("title"=>"daisy", "price"=>0.75 , "number"=>25),
array("title"=>"orchid", "price"=>1.15 , "number"=>7)
);
?>
<?php if (count($shop) > 0): ?>
<table>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($shop))); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($shop as $row): array_map('htmlentities', $row); ?>
<tr>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
any advice
thanks
It will be great to normalize your data and to avoid any comma separated lists in your database. However you can achieve this very easily using explode():
Input:
mysql> select * from marks;
+----+------+-------------+-------------------------+----------+
| id | name | school | subjects | marks |
+----+------+-------------+-------------------------+----------+
| 1 | Adam | High School | Geography,physics,math | 60,50,40 |
| 2 | Jone | Elementary | Math,Language,Geography | 90,20,10 |
+----+------+-------------+-------------------------+----------+
2 rows in set
PHP code:
<?php
$con = mysqli_connect('localhost','root','','test') or die(mysqli_error($con));
$query = "SELECT * FROM marks";
$result = mysqli_query($con,$query) or die(mysqli_error($con));
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$subject = explode(',',$row['subjects']);
$marks = explode(',',$row['marks']);
echo 'Hello '.$row['name'].'! This is your results:';
echo '<table>';
foreach($subject as $k=>$s){
echo '<tr>
<td>'.$s.'</td>
<td>'.$marks[$k].'</td>
</tr>';
}
echo '</table>';
}
}
?>
Output is table for each user:
Hello Adam! This is your results:
Geography 60
physics 50
math 40
Hello Jone! This is your results:
Math 90
Language 20
Geography 10
I have the following database table:
AWARD_ID | NOMINEE_ID | VOTER_ID | MULTI_CODE
------------------------------------------------------
5 | 3 | 1 | 9326
5 | 4 | 1 | 9326
5 | 5 | 3 | 8746
I need to display these results in tables grouped by MULTI_CODE, so for example:
So it would look like
<h1>Multi Code: 9326</h1>
<table>
<tr>
<td>Nominee: 3</td><td>Nominee: 4</td>
</tr>
</table>
<h1>Multi Code: 8746</h1>
<table>
<tr>
<td>Nominee: 5</td>
</tr>
</table>
Here is my SQL + PHP so far:
$nomineedetails = mysqli_query($con,"SELECT AWARD_ID, NOMINEE_ID, VOTER_ID, MULTI_CODE
FROM b_awards_votes WHERE AWARD_ID = '5'");
$multi_code = -1;
while($row = mysqli_fetch_array($nomineedetails))
{
$awardID = $row['AWARD_ID'];
$nomineeID = $row['NOMINEE_ID'];
$voterID = $row['VOTER_ID'];
$multiCode = $row['MULTI_CODE'];
print "<h2>$multiCode</h2>";
if ($multi_code != $multiCode) {
print "<table><tr>";
$multi_code = $multiCode;
}
print "<td>Nominee: $nomineeID</td>";";
}
</tr></table>
With this I get this:
8746
9326
Nominee: 3
9326
Nominee: 4 Nominee: 5
Why am I getting the 9326 above the Nominee 3?
First of all, I suggest to order your results by MULTI_CODE to have the correct sorting when looping through the results.
Then, in your loop you always print the headline with the multi code, regardless being different to the previous one.
Please have a look at this code. It isn't tested, so please be aware that there might be errors in it:
$nomineedetails = mysqli_query($con,"SELECT AWARD_ID, NOMINEE_ID, VOTER_ID, MULTI_CODE
FROM b_awards_votes WHERE AWARD_ID = '5' ORDER BY MULTI_CODE ASC");
$multi_code = -1;
while($row = mysqli_fetch_array($nomineedetails))
{
$awardID = $row['AWARD_ID'];
$nomineeID = $row['NOMINEE_ID'];
$voterID = $row['VOTER_ID'];
$multiCode = $row['MULTI_CODE'];
if ($multi_code != $multiCode) {
if($multi_code !== -1) {
print "</table>";
}
print "<h2>$multiCode</h2>";
print "<table>";
$multi_code = $multiCode;
}
print "<tr><td>Nominee: $nomineeID</td></tr>";
}
I am trying to fetch data from my database and would like to group common values in the column called order_ids by that id.
This is the state I currently get my data in
Order_Id | Product Name
-------------------------------
10001 | iPhone 5
10001 | Blackberry 9900
10002 | Galaxy S
10003 | Rhyme
10004 | Google Nexus
10005 | Razr
10006 | iPad Air
And this is the state I want to get it in
Order_Id | Product Name
-------------------------------
10001 | iPhone 5
Blackberry 9900
10002 | Galaxy S
10003 | Rhyme
10004 | Google Nexus
10005 | Razr
10006 | iPad Air
Here is how I get the result in my controller file
foreach($results_query as $results_custom) {
$this->data['result_custom'][] = array(
'model' => $results_custom['product_name'],
'order_number' => $results_custom['order_id']
);
}
Here is how I display it in my view file
<?php foreach ($results_custom as $result) { ?>
<li><?php echo $result['model']; ?></li> <br />
<li><?php echo $result['order_number']; ?></li><br />
<?php } ?>
Is it possible to get my data to display like that or in that state by using SQL or PHP? Please let me know if you want to see my query as well.
In php would be easier to do it. As I don't have PHP enviroment to test it I will show you some logic to do it. Not necessarily working code. Thats because you didn't provide what you did
<?
$sql = "select order_id, product name from ..... order by order_id"// rest of sql code ....
//here you iterate your results
$previousId = ""; //var to store previous id
while( $fetch... ){
if ( $fetchedID != $previousId ){
echo $fetchedId . "-" . $fetchedProductName;
}else{
echo $fetchedProductName;
}
$previousId = $fetchedID;
}
?>
This should do.
As you updated your code this is a solution for you:
<?php
$lines = ""; //to make code cleaner;
$previousModel = "";
foreach ($results_custom as $result) {
if ( $previousModel != $result['model'] ){
$line .= "<li>" . $result['model'] . "</li>";
}else{
$line .= "<li></li>";
}
$line .= "<li>" . $result['model'] . "</li><br />";
$previousModel = $result['model'];
}
echo $line;
<?php } ?>
I suggest you to use GROUP_CONCAT for getting the result
You try as follows
SELECT order_id,GROUP_CONCAT(product_name) as product_name FROM your_table GROUP BY order_id
Just look at this http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
Note : GROUP_CONCAT has a size limit. Check this link for more MySQL and GROUP_CONCAT() maximum length
you might be able to accomplish this in just MySQL, but it might be easier if you just create a php loop. most people prefer a foreach loop, but I like while loops:
$orderid = "number";
$order_query = mysql_query("SELECT * FROM ordertable WHERE Order_Id = '$orderid'");
while($order_data = mysql_fetch_array($order_query)){
$ordername = stripslashes(mysql_real_escape_string($order_data['Product Name']));
echo $ordername.'<br />';
}
if you need this for ALL orders you could remove searching for a specific order:
$order_query = mysql_query("SELECT * FROM ordertable ORDER BY Order_Id ASC");
while($order_data = mysql_fetch_array($order_query)){
$orderid = mysql_real_escape_string($order_data['Order_Id']);
$ordername = stripslashes(mysql_real_escape_string($order_data['Product Name']));
echo 'ID#: '.$orderid.' - '.$ordername.'<br />';
}