how to transfer value to another page with link? - php

Hello i'm stil learning, using Codeigniter can someone tell me, or give example code?
what i need is in Round Id we have 111 i want give it link and search database with value 111 how to do that? here the code i tried but still not right
<div class="row" id="ajaxdata">
<table border="1">
<tr>
<th>Round Id</th>
<th>Player Id</th>
<th>Bet Place</th>
<th>Total Bet</th>
<th>Win</th>
<th>Lose</th>
</tr>
<?php foreach ($tbl_bet_spot as $data) {?>
<tr>
<td><?php echo $data->round_id;?>
<td><?php echo $data->id;?></td>
<td><?php echo $data->bet;?></td>
<td><?php echo $data->total_bet;?></td>
<td><?php echo $data->win;?></td>
<td><?php echo $data->lose;?></td>
</tr>
<?php } ?>
</table>
</table>
</div>
controller
public function detail_round_id(){
$select = $_GET['select'];
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($select)->result();
print_r ($data);
}
i just try with my code and it work now, but it's static in here
<td><?php echo $data->round_id;?>
how i can send this value <?php echo $data->round_id;?> properly into controller? thanks a lot.

Use this code
<td><?php echo $data->round_id;?></td>
controller
public function detail_round_id(){
$select = $this->uri->segment(3);
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($select)->result();
print_r ($data);
}

Try this may help you,
In view make link like this,
<td><?php echo $data->round_id;?>
And in controller add parameter like this,
public function detail_round_id($id){
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($id)->result();
print_r ($data);
}

view page you pass value like this
<?php echo $data->round_id;?>
In controller get value like this
$select=$this->uri->segment(4);
hope this will help

Related

how to concatinate two variables

I have table that contain data
<td><?php echo $users->aa_0?></td
<td><?php echo $users->aa_1?></td>
<td><?php echo $users->aa_2?></td>
<td><?php echo $users->aa_3?></td>
when i'm applying static value to above code, it is working
But when i'm changing it to dynamic the below code is not working...
kindly help me
<?php foreach ($get_users as $users) { ?>
<tr>
<td><?php echo $users->aa_.$i?></td>
<?php ?>
</tr>
<?php } ?>
Basically
$users->{"aa_$i"}
But as JJ said, you should use arrays, like
$users->aa[$i]

giving result with repetition of three times for the same. CodeIgniter PHP MySQLi

This query giving same result 3 times. i couldn't identify the error. I am using codeigniter. The query in the function in model is
Model
public function combinedFunctionTest($userID){
$this->db->from('test_report');
$this->db->join('assign_tble','test_report.scode=assign_tble.scode','LEFT');
$this->db->join('course_details','test_report.ccode=course_details.ccode','LEFT');
$this->db->where('test_report.scode',$userID);
$query=$this->db->get();
return $query->result();
}
Controller
public function results()
{
$this->load->model('AllCourses_m');
$data['records']=$this->AllCourses_m-> combinedFunctionTest('2420DC');
$this->load->view('courses/TestResult',$data);
}
View
<?php
foreach ($records as $rec) {
?>
<tr>
<td>
<?php echo $rec->ccode; ?>
</td>
<td><?php echo $rec->cname; ?></td>
<td><?php echo $rec->adate; ?></td>
<td><?php echo $rec->at_date; ?></td>
<td>
<?php echo $rec->score; ?>
</td>
</tr>
<?php
}
?>
Why this is resulting three times repetition for the same. anybody can help me?

Pass value while submitting a form button from model to controller

I am making an application in codeigniter. I am trying to pass value stored in a variable when a submit button is being clicked from model to controller.
How can I achieve this? I have tried the below code so far:
model.php
public function loadadd($mekhala_Id) {
echo form_open('Payment/amount($count)');
$query = $this->db->get_where('tb_unit', array('mandalam_Id' => $mekhala_Id));
echo $count= $query->num_rows();
?>
<h1>Members List</h1>
<table border="1">
<tr>
<th>Unit</th>
<th>Unit Secretary</th>
<th>Amount paid</th>
</tr>
<?php
foreach ($query->result() as $row)
{
//$i=1;
?>
<tr>
<td> <?php echo $row->unitName ;?></td>
<td> <?php echo $row->unit_sec ;?></td>
<td> <?php echo form_input(array('name'=>'na','placeholder'=>'Rupees Paid')) ;?></td>
<td><?php echo form_checkbox(array('name'=>'check','value'=>'paid')) ; ?></td>
</tr>
<?php
// $i++;
}
// echo $query->result();
echo form_submit(array('name'=>'sub','value'=>'submit'));
echo form_close();
}
public function loadpayment($paid,$count){
for($i=1;$i<=$count;$i++)
{
$a='na'.$i;
$paid=array($this->input->post($a));
$this->db->insert('tb_unit',array('Amount'=> $paid));
}
}
}
?>
controller.php public function amount($count) {
$paid=$this->input->post('na');
$this->cms_model->loadpayment($paid,$count);
}
While running this code below is being shown:
An error was encountered
The URI you submitted has disallowed characters.
I think you should read again the main idea of the model, view and controller.
The idea of the models is only to work with the database. You do not use form_open() or other html in models, this should be done in the views.
This tutorial shows more details.
To pass value with submit you can use hidden field in the form as show here.
To pass data in a variable when submit button is being clicked: you can set the variable to the value attribute: echo form_submit(array('name'=>'sub','value'=>"$variable"));

PHP table from JSON without names in array

I am having a problem with api Yandex.
I have to get data from api and make with php the table:
Source Users New Pages Bounce Goal1 Goal2
organic s1v1 s1v2 s1v3 s1v4 s1v5 s1v6
referral s2v1 s2v2 s2v3 s2v4 s2v5 s2v6
(none) s3v1 s3v2 s3v3 s3v4 s3v5 s3v6
After json_decode I have:
{"data":[
{"dimensions":[{"name":"organic"}],"metrics":[s1v1,s1v2,s1v3,s1v4,s1v5,s1v6]},
{"dimensions":[{"name":"referral"}],"metrics":[s2v1,s2v2,s2v3,s2v4,s2v5,s2v6]},
{"dimensions":[{"name":"(none)"}],"metrics":[s3v1,s3v2,s3v3,s3v4,s3v5,s3v6]},
]}
But I can't correctly parse it into a table. So far I have written code only for the first column Source, and then stuck:
<?php
$metrika_o = json_decode($metrika);
echo "<table>
<tr>
<td><strong>Source</strong></td>
<td><strong>Users</strong></td>
<td><strong>New</strong></td>
<td><strong>Pages</strong></td>
<td><strong>Bounce</strong></td>
<td><strong>Goal1</strong></td>
<td><strong>Goal2</strong></td>
</tr>";
foreach($metrika_o->data as $data)
foreach($data->dimensions as $source)
:
?>
<tr>
<td><?php echo $source->name?></td>
</tr>
<?php endforeach;
echo "</table>";
?>
The Source number is constantly changing, the set of columns is fixed.
Please help me to solve this task
You are on the right way:
<?php foreach($metrika_o->data as $data): ?>
<tr>
<td><?php echo $data->dimensions[0]->name; ?></td>
<?php foreach(explode(',',$data->metrics) as $col):
<td><?php echo $col; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>

How iterate a fetch_assoc() array?

Im a very novice programmer, i work with PHP and a have a doubt/problem with fetch_assoc().
I have this method that return the result of the database consult:
public function getUsuarios() {
try {
$resultado = $this->conexion->query("SELECT * FROM login_usuarios");
} catch(Exception $e) {
throw new Exception("Error al obtener los usuarios de la base de datos. Código de error: ".mysqli_errno);
} finally {
if($this->conexion != null) {
$this->conexion->close();
}
}
return $resultado;
}
This script iterate and show the data of the database consult's result:
<table>
<tr>
<td>Código</td>
<td>Nombres</td>
<td>Apellidos</td>
</tr>
<?php while ($fila = mysqli_fetch_array($usuarios)) { ?>
<tr>
<td><?php print($fila['codUsuario']); ?></td>
<td><?php print($fila['nombres']); ?></td>
<td><?php print($fila['apellidos']); ?></td>
</tr>
<?php
}
$usuarios->free();
?>
</table>
With this way, all worked fine (but i don't understand what xD, i've see this way in a forum).
But, i've see other way in many sites. With this way the script stand like this:
// first convert the result to a fetch_assoc
$usuarios = (new Consultas)->getUsuarios()->fetch_assoc();
<table>
<tr>
<td>Código</td>
<td>Nombres</td>
<td>Apellidos</td>
</tr>
<?php foreach ($usuarios as $fila) { ?>
<tr>
<td><?php print($fila["codUsuario"]); ?></td>
<td><?php print($fila["nombres"]); ?></td>
<td><?php print($fila["apellidos"]); ?></td>
</tr>
<?php
}
$usuarios->free();
?>-->
</table>
But with this way i have a Illegal string offset mysql. This looks like the foreach iterate on each column and not on each row.
Why the first way works and the second way don't?, How works mysqli_fetch_array($result) and fetch_assoc()?
Thanks for help.
Construction finally closes connection before your call fetch_assoc().
Try to comment $this->conexion->close();.
Your code iterates through row. mysqli_result::fetch_assoc return only one row. Please, read the documentation.

Categories