I am trying to display a value $raw which is in an array format to a textbox in a CodeIgniter view
This is the function I am using in my controller:
public function pricing($raw='array')
{
if ($raw === 'array')
{
return $this->result_array();
}
}
I tried this but still getting message:array to string conversion
I am using $raw in my view 'add_calendar.php':`
<?php echo(['name'=>'tohr','class'=>'form-control','placeholder'=>'Enter total hire rate','value'=>'$raw'])?>
You can not echo an array because echo expects string as its parameter. Since you are trying to echo the values to a textbox you may want to do this.
When you load your view pass $raw to your view.
$data = array('raw'=>$raw);
$this->load->view('add_calendar', $data);
Then in your view use form_input to create a textbox.
echo form_input(['name'=>'tohr','class'=>'form-control','placeholder'=>'Enter total hire rate','value'=>$raw]);
Pass the array from the controller to view
$raw is a variable don't use single quotes ' for it
$data['record'] = array('name'=>'tohr','class'=>'form-control','placeholder'=>'Enter total hire rate','value'=> $raw);
return $this->load->view('file', $data);
View
You can use these values with or without HTML tags
Without HTML tags
<?= $name ?> //tohar
<?= $class ?> //form-control
<?= $raw ?>
With HTML tags
<td><?= $name ?></td>
<p><?= $name ?></p>
<h3><?= $name ?></h3>
Related
I'm not sure how to search this, but I have an array in php numbered 1 to 20. I have a foreach loop to output the values and have the user be able to click on the numbers. After clicking the number, the page would then output the number clicked.
HTML:
<div id="chapters" onclick="getChapter()">
$array = range(1,20);
foreach($array as $chapter) {
?>
<p class="getChapter" id="currChapter">Chapter <?php echo $chapter;?></p>
<?php
}
?>
</div>
Javascript
function getChapter() {
chapter = document.getElementById("currChapter").id;
document.write(chapter);
}
I'm not able to output the number that the user clicks on.
I have tried putting
id=<?php $chapter?>
, but that does not work as well as replacing id with value and name.
I would recommend passing the $chapter as parameter of a function "Onclick" event :
<div id="chapters">
<?php
$array = range(1,20);
foreach($array as $chapter) {
?>
<p class="getChapter" onClick="getChapter(<?php echo $chapter;?>)">Chapter <?php echo $chapter;?></p>
<?php
}
?>
and the Javascript:
function getChapter(chapterNumber) {
alert(chapterNumber);
}
I don't really understand what you want to do, seems you explanation just a example, but may be this will help. You can call function getChapter with parameter ( onClick="getChapter(<?php echo $chapter;?>) ) :
$array = range(1,20);
foreach($array as $chapter) {
?>
<p class="getChapter" id="currChapter" onClick="getChapter(<?php echo $chapter;?>)">Chapter <?php echo $chapter;?></p>
<?php
}
function getChapter(chapter) {
document.write(chapter);
}
Here is my code I need how to add a percentage or number to each value in total field I have tried a lot but nothing works.
<?php
$json=file_get_contents("http://www.upliftinghumanity.net/edd-api/sales/?key=75caa8cb60362c89e6ac2b62730cd516&token=6547d40b82cecb81cb7b0ec928554a9e&number=-1");
$data = json_decode($json);
extract(json_decode($json, true));
if (count($data->sales)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->sales as $idx => $sales)
{
// Output a row
echo "<tr>";
echo "<td>$sales->total</td>";
echo "<td>$sales->total+3 </td>";
echo "<td>$sales->gateway</td>";
echo "<td>$sales->email</td>";
echo "<td>$sales->transaction_id</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
?>
result image
You can add it before echoing it out as a string for example $somevar = $sales->data+1; echo "blahh $somevar"; or echo "blahh {$somevar}";
#Ezekiel the problem is solved what you suggest. i was missing some basic stuff . Thanks –
Hi you can't perform php arithmetic operations which its been qouted as a string, you can probably use the "{$sales->data+3}" but it is always advisable to do thr calculation outside the string as this is only a pseudo code "{$sales->data+3}" and may not work even if you include the curly braces
I have a form on an HTML page which posts a user input to a PHP which executes a SQL statement to get information from a database and output it in JSON. Here is my PHP code to get the JSON data:
<?php
header('Content-type:application/json');
$con = mysqli_connect('localhost','root','','dbname');
if(isset($_POST['submit']))
{
$artist = $_POST['artist'];
$select = mysqli_query($con, "SELECT * FROM tblname WHERE artistName = ". $artist);
$rows = array();
while($row = mysqli_fetch_array($select))
{
$rows[] = $row;
}
echo json_encode($rows);
};
?>
When I submit the form on the HTML page, it goes to the PHP page and I get this result:
[{"0":"6","id":"6","1":"OASIS","artistName":"OASIS","2":"SOME MIGHT SAY","songTitle":"SOME MIGHT SAY","3":"1995-05-06","dateNo1":"1995-05-06"}]
(Code and JSON result have been simplified for this question)
Instead of going to this page, I'd like the data to be displayed nicely on the HTML page so that a user does not see this array. I'm not sure how to do this, any help?
Thank you!
If you want the output formatted nicely, why are you encoding in JSON? JSON, although human-readable, isn't intended to be rendered on-screen as an end product.
I'd recommend looping through the $rows array and building a simple HTML table. You could, of course, wrap the data in any markup you like and even add CSS to style it as you see fit. Here's an example to get you started:
echo "<table>";
foreach($rows as $row) {
foreach($row as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
If you only want some of the data rendered, you can simply echo those values:
foreach($rows as $row) {
{
echo "Artist:" . $row['artistName'];
echo "<br>";
echo "Song Title:" . $row['songTitle'];
echo "<br>";
echo "Date:" . $row['dateNo1'];
}
There are two ways to solve this problem :
Perform AJAX call from Javascript to PHP Page and parse the success response in to the HTML.
Simply echo the values in PHP Page or add HTML itself in PHP Page as suggested by Tim.
So. I'm making a Forum, and I want to build it with using AngularJS.
First off.
Now, I've set up a connection to my database with:
<?php
session_start();
$db = new mysqli("localhost","root","","uf") or die ("ERROR! With connection!");
function sql($query){
return mysql_query($query);
}
$sql = "SELECT forum_id, forum_name, forum_desc, forum_created FROM forum_tbl";
if($query = $db->prepare($sql)){
$query->bind_result($f_id, $f_name, $f_desc, $f_created);
$query->execute();
$query->store_result();
}else{
echo $db->error;
}
?>
It's separated to 2 files, First being db_connect.php and second core.php
I've made the tables above, they have sample data.
How can I now make the f_id, f_name,.. to JSON encoded data? And use that JSON in my main.js file to use data.forumid?
I have made php to read the data from the database using:
<?php if($query->num_rows !==0):
while($row = $query->fetch()):
?>
<tr>
<td><?= $f_id;?></td>
<td><?= $f_name;?></td>
<td><?= $f_desc;?></td>
<td><?= $f_created;?></td>
</tr>
<?php endwhile; endif; ?>
BUT I do NOT want to read the data like that.
I would love to use ng-repeat.
You should store the rows in an array and then use json_encode() to convert it to json:
$rows = array();
while($row = $query->fetch()) {
$rows[] = $row;
}
echo json_encode($rows);
So.
I did this with....
<a ng-click="setRoute('forum')" href="#/forum/id=<?php echo $f_id;?>" class="list-group-item">Link</a>
And in my html is ofcourse the ng-app="urheilu"
var app = angular.module('urheilu', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
.when('/forum/id=:id', {
templateUrl: function(urlattr){
return 'app/components/forums/threadlist.php?id=' + urlattr.id + '';
}
})
});
Thats how I did my dynamic linking.
DONE.
-Kevin
i need to get the value of a variable in a javascript function to put it in a textfiled.
Javascripts function:
function obtenerArrendatario(){
arrendatario_id = $.fn.yiiGridView.getSelection('arrendatario');
alert(arrendatario_id);
}
The alert give me the id of the selected row but i need to put the id in the textfield to save/create the form
Gridview
<div class="row">
<?php echo $form->labelEx($model,'zf_arrendatarios_arrendatario_id'); ?>
<?php
$arrendatarios = new ZfArrendatarios;
$this->widget('bootstrap.widgets.TbGridView',
array(
'id'=>'arrendatario',
'selectableRows'=>1,
'selectionChanged'=>'obtenerArrendatario',
'type'=>'striped bordered condensed',
'dataProvider'=>$arrendatarios->search(),
'filter' => $arrendatarios,
'template'=>"{items}\n{pager}",
'columns'=>array(
array('name'=>'arrendatario_id_personal', 'header'=>'DNI',),
array('name'=>'arrendatario_nombre', 'header'=>'Nombre'),
array('name'=>'arrendatario_email', 'header'=>'Email'),
),
));
?>
<?php
echo $form->textfield($model,'zf_arrendatarios_arrendatario_id',array('class'=>'input input_r input_pryk', 'value'=>$arrendatario));
?>
<?php echo $form->error($model,'zf_arrendatarios_arrendatario_id'); ?>
</div>
So i need that id that the function capture to fill my textfield.
I tried
$arrendatario= "<script> document.write(arrrendatario_id) </script>";
But it print me the string not the value.
For your requirement, you can set the textfield value in below js function
function obtenerArrendatario(){
arrendatario_id = $.fn.yiiGridView.getSelection('arrendatario');
$(".input_pryk").val(arrendatario_id );// input_pryk should be class for this text field alone.
}
Then you will get in controller while submitting the form.