On my site, users can create teams (clans) and invite other users to join them.
I’m working on a page at the moment where team leaders can edit the “ranks” of each user in the team. These options are Founder, Captain, Member, Inactive and Kick (which will remove the user from the team altogether).
I’ve included a quick screenshot of how the page looks to help explain what I’m hoping to achieve.
http://cl.ly/E9Gb
So team leaders can go through and change the rank of each user and then hit update.
What would be the best way to go about this?
Here are some extracts from my code at present:
CONTROLLER:
if ($this->input->post('update_roster'))
{
$member_id = $this->input->post('user_id');
$member_rank = $this->input->post('member_rank');
$this->clan_model->update_roster($clan_id, $member_id, $member_rank);
}
MODEL:
public function update_roster($clan_id, $user_id, $rank)
{
$data = array(
'rank' => $rank
);
$this->db->where('clan_id', $clan_id);
$this->db->where('user_id', $user_id);
if ($this->db->update('clan_joined', $data))
{
return TRUE;
}
}
VIEW:
<h3>Roster</h3>
<?php
$member_rank = array(
'1' => 'Founder',
'2' => 'Captain',
'3' => 'Member',
'4' => 'Inactive',
'5' => 'Kick'
);
?>
<?php $attributes = array('id' => 'update_roster', 'class' => 'nice'); ?>
<?php echo form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?>
<table width="100%">
<thead>
<tr>
<th class="text-center">#</th>
<th>Username</th>
<th>Rank</th>
</tr>
</thead>
<?php form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?>
<?php foreach ($members AS $member) : ?>
<tr>
<td class="text-center"><?php echo avatar(32, $member->username, $member->avatar_uploaded); ?></td>
<td><?php echo $info->clan_tag; ?> / <?php echo $member->username; ?></td>
<td><?php echo form_dropdown('member_rank', $member_rank, $member->rank); ?></td>
</tr>
<?php echo form_hidden('user_id', $member->username); ?>
<?php endforeach; ?>
</table>
<?php echo form_submit('update_roster', 'Update'); ?>
<?php echo form_close(); ?>
I understand this code is wrong, as it's not working, but thought I would share what I have done so far.
On your HTML form change the dropdown and your hidden input to be arrays:
<td><?php echo form_dropdown('member_rank[]', $member_rank, $member->rank); ?></td>
and
<?php echo form_hidden('user_id[]', $member->username); ?>
Then $this->input->post('member_rank') and 'user_id' will each receive an array. You can loop through the arrays in PHP and call the UPDATE code for each iteration of the loop:
for ($i = 0; $i < count($member_id); $i++) {
$current_member_id = $member_id[$i];
$current_member_rank = $member_rank[$i];
// Call your update code.
}
Related
This code shows every info from an XML. I want to only display row number one (first from/to, symbol temperature etc)
<?php
$url = ('https://www.yr.no/sted/Norge/oslo/oslo/oslo/varsel_time_for_time.xml');
$feed = simplexml_load_file($url) or die('Can not connect to server');
$result = array();
foreach ($feed->forecast->tabular->time as $content) {
array_push($result, [ "from" => (string)$content['from'],
"to" => (string)$content['to'],
'symbol' => (string)$content->symbol['name'],
'symbol-icon' => (string)$content->symbol['var'],
'temperature' => (string)$content->temperature['value'],
'windDirection' => (string)$content->windDirection['name'],
'windSpeed' => (string)$content->windSpeed['mps'],
'windType' => (string)$content->windSpeed['name'],
]);
}
?>
<button class="collapsible"><div class="tbl-content">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<?php foreach ($result as $value) { ?>
<tr>
<td>Oslo</td>
<td><?php echo date("j. M", strtotime($value['from'])); ?> kl.<?php echo date("G", strtotime($value['from'])); ?></td>
<td><img src="http://yr.github.io/weather-symbols/png/100/<?php echo $value['symbol-icon'];?>.png" /></td>
<td><?php echo $value['temperature'] ?> °C</td>
<td><?php echo $value['windType'] ?>, <?php echo $value['windSpeed'] ?> m/s fra <?php echo $value['windDirection'] ?></td>
<td>Longtherm</td>
<td>Hour</td>
</tr>
<?php } ?>
</tbody>
</table>
</div></button>
quite unexperienced, any help is greatly appreciated
Just one of the rows? Remove the foreach! Just use $result[0] in place of $value. :-)
A defined number of rows? Use a for loop:
for ($x = 0; $x < $limit; $x++) {
$value = $result[$x];
// etc
}
I unable to delete specific record from session array. I want to delete specific row in table when I click on delete link.
<?php
session_start();
if(isset($_GET["product"]) && isset($_GET["category"])){
$nomProduct = trim($_GET["product"]);
$category = trim($_GET["category"]);
$_SESSION['product'][] = array(
"nomProduct" => $nomProduct ,
"category" => $category
);
//session_destroy();
}
?>
html table
<table class="table">
<?php foreach($_SESSION["product"] as $items) { ?>
<tr>
<th width="250px"><?php echo $items['nomProduct']; ?></th>
<td><?php echo $items['category']; ?></td>
<td style="text-align: right">Delete<td>
</tr>
<?php }?>
</table>
`
$key=array_search($_GET['product'],$_SESSION['product']);
if($key!==false)
unset($_SESSION['product'][$key]);
$_SESSION["product"] = array_values($_SESSION["product"]);
`
Maybe this might help!
You need to find the key as this is an array.
EDIT:
Made an example for you, here when you click the link, it deletes the first name from the session array.
<?php
session_start();
$_SESSION["user"] = ["fname"=>"William","lname"=>"Henry" ];
if(isset($_GET["delete"]))
{
if($_GET["key"])
{
$key=$_GET["key"];
unset($_SESSION['user'][$key]);
}
}
?>
HTML on the same page
<h1>
<?php
if(isset($_SESSION["user"]["fname"]))echo $_SESSION["user"]["fname"]." ";
if(isset($_SESSION["user"]["lname"]))echo $_SESSION["user"]["lname"];
?>
</h1>
Delete First Name
If you want to delete the lastname (lname), change the key=lname in the href of the link, hope this example helps in your case
Modify your HTML
<table class="table">
<?php foreach($_SESSION["product"] as $key => $items) { ?>
<tr>
<th width="250px"><?php echo $items['nomProduct']; ?></th>
<td><?php echo $items['category']; ?></td>
<td style="text-align: right">Delete<td>
</tr>
<?php }?>
</table>
Catch the array key and remove it from session array.
$key = filter_input(INPUT_GET, 'key');
unset($_SESSION['product'][$key]);
im having trouble getting a result value of a query and the error says.
Notice: Undefined index: prelim in C:\xampp\htdocs\gradingxworking\teacher\student.php on line 85 can someone help me to fix this or some clue to fix this? im just starting to learn php.
<tbody>
<?php $c=1; ?>
<?php foreach($mystudent as $row): ?>
<tr>
<td><?php echo $c; ?></td>
<td class="text-center"><?php echo $row['studid']; ?></td>
<td class="text-center"><?php echo $row['lname'].', '.$row['fname']; ?></td>
<?php $grade = $student->getstudentgrade($row['studid']);?>
//code that cause error line 85--> <td class="text-center"><?php echo $grade['prelim']; ?></td>
</tr>
<?php $c++; ?>
<?php endforeach; ?>
<?php if(!$mystudent): ?>
<tr><td colspan="8" class="text-center text-danger"><strong>*** No Result ***</strong></td></tr>
<?php endif; ?>
</tbody>
the function:
function getstudentgrade($studid){
$q = "select * from studentsubject where studid=$studid";
$r = mysql_query($q);
$data = array();
while($row = mysql_fetch_array($r)){
$data[] = array(
'prelim' => $row['prelim']
);
}
return $data;
}
As #Bara suggested, you need to check the array first before accessing it.
if(isset($grade['prelim']))
I suspect, there is no data for specific studid. Lets see you function.
$data = array();
while($row = mysql_fetch_array($r)){
$data[] = array(
'prelim' => $row['prelim']
);
}
Now, you have created new array $data. But, if there is no record ? your while loop won't be executed and your $data array won't have anything. right ? So, to handle that, you need to check whether there is any data in your array.
Now, second point, which #Mossavari made is also correct. You need to use
$grade[0]['prelim'];
instead of
$grade['prelim'];
after doing
<?php $grade = $student->getstudentgrade($row['studid']);?>
you need to check what $grade holdes. and its better before trying to fetch data from array to make a check like this :
if(isset($grade['prelim']))
Based on your getstudentgrade function you'll have multidimensional array result, you may need to change $grade['prelim'] to $grade[0]['prelim']
Since, every student will have only 1 grade. So, why to use array.
<?php
function getstudentgrade($studid){
$q = "select * from studentsubject where studid=$studid LIMIT 0,1";
$data = "";
$r = mysql_query($q);
while($row = mysql_fetch_array($r)){
$data = $row['prelim'];
}
return $data;
}?>
PHP
<tbody>
<?php $c=1; ?>
<?php foreach($mystudent as $row): ?>
<tr>
<td><?php echo $c; ?></td>
<td class="text-center"><?php echo $row['studid']; ?></td>
<td class="text-center"><?php echo $row['lname'].', '.$row['fname']; ?></td>
<td class="text-center"><?php echo $grade = $student->getstudentgrade($row['studid']);?></td>
</tr>
<?php $c++; ?>
<?php endforeach; ?>
<?php if(!$mystudent): ?>
<tr>
<td colspan="8" class="text-center text-danger">
<strong>*** No Result ***</strong>
</td>
</tr>
<?php endif; ?>
</tbody>
i have a web project using codeigniter.
i have a problem with searching in my project. i have to show the multiple result from searching page with some keyword.
here is my Model
function find_user($like){
$this->db->from('user');
$this->db->like('name', $like,'after');
$result =$this->db->get();
return $result->result_array();
}
and in my user table, include
id | name | place_code
in the user table , column place_code is use to show the place from the user
here is my Controller
function search(){
$query = $this->input->post('query_cari');
$find = $this->m_user->find_user($query);
foreach ($find as $key) {
$code = $key['place_code'];
if ($code == '1') {
$place = 'Secretray';
}elseif($code == '2'){
$place = 'OB';
}elseif($code == '3'){
$place ='Manager';
}
}
$data['result'] = $find;
$data['place'] = $place;
$this->load->view('home/search',$data);
}
that's my code for controller, include a logic for get the position from user in office. but the problem is, when i get a result just 1 result, the place is right. but if i get more than 1 result, place is going wrong and just show the place for all result is the place from the last result in searching.
what i want is, all result just shown their own place.
here my VIEW
<?php foreach ($find as $key: ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $key['id'] ?></td>
<td><?php echo $key['name'] ?></td>
<td><?php echo $place ?></td>
</tr>
</tbody>
</table>
<?php endforeach ?>
It is normal that you always get the last place because your logic is not correct. First, you have an error in your model. Secondly, you can improve your model code:
function find_user($like)
{
$this->db->like('name', $like, 'after');
return $this->db->get('user')->result();
}
Now, in your controller you want to change the variable place according to the value of place_code. You are allowed to add a new key (or change an existing one) in real time to the stdClass() as follows:
foreach($find as $i => $result)
{
// By default, we assume the 'place_code' is equal to '1'
$find[$i]->place = 'Secretray';
if($result->place_code == '2')
$find[$i]->place = 'OB';
else
$find[$i]->place = 'Manager';
}
$data['find'] = $find;
$this->load->view('home/search', $data);
Finally, in your view:
<?php foreach ($find as $result){ ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $result->id; ?></td>
<td><?php echo $result->name; ?></td>
<td><?php echo $result->place; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
I have table in my view. This table is in form. I need send all items from table to my controller. I create form but he is sending only one item, last item...
Here is my table in View:
<?php echo $this->Form->create('Goodsandoffer');?>
<table id="GoodSandOffersTable" class="display" cellspacing="0" width="100%">
<thead><tr>
<th><?php echo $this->Paginator->sort('id','ID'); ?></th>
<th><?php echo $this->Paginator->sort('nazwa','Nazwa'); ?></th>
<th><?php echo $this->Paginator->sort('kodKreskowy','Kod EAN'); ?></th>
<th><?php echo $this->Paginator->sort('cenaSprzedazyBrutto','Cena sprzedaży brutto'); ?></th>
<th>Cena Promocyjna</th>
<th>Akcja</th>
</tr>
</thead>
<tbody>
<?php $x =0;?>
<?php foreach ($goods as $good): ?>
<tr>
<td><?php echo h($goodID= $good['Good']['id']);?></td>
<td><?php echo h($good['Good']['nazwa']);?></td>
<td><?php echo h($good['Good']['kodKreskowy']);?></td>
<td><?php echo h($good['Good']['cenaSprzedazyBrutto']);?></td>
<?php echo $this->Form->input('promotionaloffer_id',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna', array('id' => 'cenaPromocyjna'.$x));?></td>
</tr>
<?php $x = $x + 1 ;?>
<?php endforeach; ?>
</tbody>
</table>
<?php echo $this->Form->end(__('Dodaj')); ?>
Here is controller:
if ($this->request->is('post')) {
debug($this->request->data);
}
And when Im debug I got this:
\app\Controller\GoodsandoffersController.php (line 110)
array(
'Goodsandoffer' => array(
'promotionaloffer_id' => '3',
'good_id' => '20',
'cenaPromocyjna' => '3'
)
)
In array I have only last item from table. I need all. What Im doing wrong?
I'm not familiar with CakePHP myself but it looks like all your fields in your table rows have the same name (promotionaloffer_id, good_id, cenaPromocyjna):
<?php echo $this->Form->input('promotionaloffer_id',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna', array('id' => 'cenaPromocyjna'.$x));?></td>
In order to receive all values, you need to indicate them with array keys like so:
<?php echo $this->Form->input('promotionaloffer_id['.$good['Good']['id'].']',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id['.$good['Good']['id'].']',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna['.$good['Good']['id'].']', array('id' => 'cenaPromocyjna'.$x));?></td>
This would result in input names like
promotionaloffer_id[0], good_id[0], cenaPromocyjna[0]
promotionaloffer_id[1], good_id[1], cenaPromocyjna[1]
...
promotionaloffer_id[x], good_id[x], cenaPromocyjna[x]
Where x is the good-id.