data i am fetching from infusion using tag id and dsfind() function. and that data i have to divide into 3 pages. (data used be unique)
<?php
require("isdk.php");
$app = new iSDK;
if ($app->cfgCon("infusionid"))
{
$returnFields = array('Id','Email');
$contacts = $app->dsFind('Contact',14,0,'Groups',7406,$returnFields);
echo '<pre>', print_r($contacts), '</pre>';
}
?>
I want to print this series
<tr>
<td><?php echo $contacts[0]['Id'] ?></td>
<td><?php echo $contacts[0]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[4]['Id'] ?></td>
<td><?php echo $contacts[4]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[7]['Id'] ?></td>
<td><?php echo $contacts[7]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[11]['Id'] ?></td>
<td><?php echo $contacts[11]['Email']; ?></td>
</tr>
You can use foreach() function e.g:
echo "<table>";
foreach ( $contacts as $var) {
echo "<tr><td>ID: ", $var['Id']."</td>";
echo "<td>Email: ", $var['Email']."</td></tr>";
}
echo "</table>";
Related
I have a little issue when I try to to loop my php values in HTML. So far this is what I tried but I have not excpected result.
If I remove the loop I only get the first entry. I would like to echo all the possibles entries from my research.
This is my code ( from an SQL request).
<html>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Number</th>
<th>Adress</th>
<th>link</th>
<!--<th class="glyphicon glyphicon-pencil"></th>-->
</tr>
<tr>
<?php $rowG = oci_fetch_array($stid, OCI_RETURN_NULLS);?>
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
</tr>
</table>
</html>
Do you know where I made my mistake ?
Thank you for your help
Edit : Finally I managed to do it by using a do{}while loop.
Thank you all for your help
RFlow
It's hard to guess what you are trying to do or even what actually happens, since I don't know what is assigned to $rowG, so I tried to hack meaning out of this from the code's errors and came up with that :
<?php
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]; ?></td>
<td><?php echo ' Link '; ?></td>
</tr>
<?php
}
?>
If it doesn't work with you, you'll have to provide the informations that should have been included in your question since the begining.
This is basic iteration over query results:
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
</tr>
<?php
}
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Would be :
<?php foreach($rowG as $arr): ?> <tr>
<td><?php echo $arr[2]; ?></td>
<td><?php echo $arr[1]; ?></td>
<td><?php echo $arr[0]; ?></td>
<td><?php echo $arr[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$arr[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Note the use of $arr instead of $rowG.
In the original code $array is not used.
Here's my controller call "a_porto"
public function index() {
$this->load->model('a_porto_models');
$data = array();
$data['tampil'] = $this->a_porto_models->get_crud_all();
$this->load->view('a_porto_view', $data);
}
Here's my model call "a_porto_models"
function get_crud_all() {
$query = $this->db->get('prototype');
return $query->result();
}
And than my view call "a_porto_views"
<?php $tampil = array(); ?>
<?php foreach ($tampil as $a){ ?>
<tr>
<td><?php echo $a->id_proto; ?></td>
<td><?php echo $a->foto; ?></td>
<td><?php echo $a->nama_pro; ?></td>
<td><?php echo $a->tahun; ?></td>
<td><?php echo $a->lokasi; ?></td>
<td><?php echo $a->deskripsi; ?></td>
</tr>
<?php } ?>
What I've to do? My code doesn't show an error :(
Change to this code:
<?php foreach ($tampil as $a){ ?>
<tr>
<td><?php echo $a->id_proto; ?></td>
<td><?php echo $a->foto; ?></td>
<td><?php echo $a->nama_pro; ?></td>
<td><?php echo $a->tahun; ?></td>
<td><?php echo $a->lokasi; ?></td>
<td><?php echo $a->deskripsi; ?></td>
</tr>
<?php } ?>
You see null result because you reset your array to be empty. when its already have content from the controller sent to the view
Remove $tampil = array(); and try again. You are basically setting your result to an empty array there.
This question already has answers here:
How to convert XML into array in PHP?
(12 answers)
Closed 7 years ago.
I've got strange xml file like this http://xxx.pl/tmp/tabela.xml, and I don't know how to convert it into an array. I tried something like this:
<?php
$get = file_get_contents('http://xxx.pl/tmp/tabela.xml');
$arr = simplexml_load_string($get);
$data = $arr -> druzyna;
?>
<table>
<tr>
<th>pozycja</th>
<th>pkt</th>
<th>mecze</th>
<th>zwyciestwa</th>
<th>porazki</th>
<th>wygrane w domu</th>
<th>przegrane w domu</th>
<th>wygrane na wyjezdzie</th>
<th>przegrane na wyjezdzie </th>
<th>kosze zdobyte</th>
<th>kosze stracone</th>
<th>stosunek zdobytych punktów do straconych punktów</th>
<th>pelna nazwa klubu</th>
<th>logo klubu</th>
</tr>
<?php foreach($data as $row) : ?>
<tr>
<td><?php echo $row->pozycja ?></td>
<td><?php echo $row->pkt ?></td>
<td><?php echo $row->mecze ?></td>
<td><?php echo $row->zwyciestwa ?></td>
<td><?php echo $row->porazki ?></td>
<td><?php echo $row->wygrane_dom ?></td>
<td><?php echo $row->przegrane_dom ?></td>
<td><?php echo $row->wygrane_wyjazd ?></td>
<td><?php echo $row->przegrane_wyjazd ?></td>
<td><?php echo $row->kosze_zdobyte ?></td>
<td><?php echo $row->kosze_stracone ?></td>
<td><?php echo $row->stosunek_zdob_strac ?></td>
<td><?php echo $row->pelna_nazwa ?></td>
<td><?php echo $row->logo; ?></td>
</tr>
<?php endforeach;?>
</table>
but it's not working.
I read something about json solution, but i don't know how to use it, however I met first time xml format like that.
I think that this topic How to convert xml into array in php? isn't solution of my problem, because i've got another xml file..
simplexml_load_string returns an SimpleXMLElement object. You can query the object (search your data) by using the xpath() function.
Because of how your XML file is set up, the values are set as attributes.
So you need to retrieve the attributes before you can read them.
<?php
$get = file_get_contents('http://xxx.pl/tmp/tabela.xml');
$arr = simplexml_load_string($get);
$data = $arr->xpath('tabela/druzyna');
?>
<table>
<tr>
<th>pozycja</th>
<th>pkt</th>
<th>mecze</th>
<th>zwyciestwa</th>
<th>porazki</th>
<th>wygrane w domu</th>
<th>przegrane w domu</th>
<th>wygrane na wyjezdzie</th>
<th>przegrane na wyjezdzie </th>
<th>kosze zdobyte</th>
<th>kosze stracone</th>
<th>stosunek zdobytych punktów do straconych punktów</th>
<th>pelna nazwa klubu</th>
<th>logo klubu</th>
</tr>
<?php foreach($data as $row) : ?>
<?php
$row = $row->attributes();
?>
<tr>
<td><?php echo $row->pozycja ?></td>
<td><?php echo $row->pkt ?></td>
<td><?php echo $row->mecze ?></td>
<td><?php echo $row->zwyciestwa ?></td>
<td><?php echo $row->porazki ?></td>
<td><?php echo $row->wygrane_dom ?></td>
<td><?php echo $row->przegrane_dom ?></td>
<td><?php echo $row->wygrane_wyjazd ?></td>
<td><?php echo $row->przegrane_wyjazd ?></td>
<td><?php echo $row->kosze_zdobyte ?></td>
<td><?php echo $row->kosze_stracone ?></td>
<td><?php echo $row->stosunek_zdob_strac ?></td>
<td><?php echo $row->pelna_nazwa ?></td>
<td><?php echo $row->logo; ?></td>
</tr>
<?php endforeach;?>
</table>
It seems like a normal xml, please try this
$url = "http://example.com/tmp/tabela.xml";
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
echo '<pre>';
print_r($xml);
This are my view code:
<?php
foreach($books as $books) {
echo $books->book_id;
echo $books->book_name;
echo $books->description;
echo $books->author;
echo $books->publisher;
echo $books->pages;
echo $books->publication_date;
echo $books->price;
echo $books->status;
echo $books->quantity;
echo $books->genres;
echo $books->user_rating;
echo $books->reviews;
}
?>
the output is
However i want the output to be like Book ID:1, Book Name:qweqweqweqweqwe, price 123.00 and so on, in a table form am i able to do it?
I hope this will help you..
<table>
<th>
<td>Book Id</td>
<td>Book Name</td>
<td>Description</td>
<td>Author</td>
<td>Publisher</td>
<td>Pages</td>
<td>Publication Date</td>
<td>Price</td>
<td>status</td>
<td>Quantity</td>
<td>Genres</td>
<td>User Rating</td>
<td>Reviews</td>
</th>
<?php foreach($books as $book) { ?>
<tr>
<td><?php echo $book->book_id ?></td>
<td><?php echo $book->book_name ?></td>
<td><?php echo $book->description ?></td>
<td><?php echo $book->author ?></td>
<td><?php echo $book->publisher ?></td>
<td><?php echo $book->pages ?></td>
<td><?php echo $book->publication_date ?></td>
<td><?php echo $book->price ?></td>
<td><?php echo $book->status ?></td>
<td><?php echo $book->quantity ?></td>
<td><?php echo $book->genres ?></td>
<td><?php echo $book->user_rating ?></td>
<td><?php echo $book->reviews ?></td>
</tr>
<?php } ?>
</table>
concat your string with php function.
echo "Book ID:".$books->book_id.", Book Name: ".$books->book_name;
Like this way.
Use like this:
echo "Book ID : $books->book_id";
double quotes will automatically identify variables and execute them.
you can also try:-
echo 'Book ID:'. $books->book_id;
Please choose which one easily readable for you.
I have pulled some information from a site API and it is stored in $result. I would like to create a table with this information that loops x number of times (constant). Here is how I currently have it repeating by hand and it works. I just imagine there must be a better/easier way to do this then manually repeating it each time.
<tr>
<td><?php echo strtoupper($result['weapons']['0']['stat']['id']); ?></td>
<td><?php echo round($result['weapons']['0']['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons']['0']['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['hs']); ?></td>
<td><?php echo round($result['weapons']['0']['extra']['hkp'],2); ?>%</td>
</tr>
<tr>
<td><?php echo strtoupper($result['weapons']['1']['stat']['id']); ?></td>
<td><?php echo round($result['weapons']['1']['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons']['1']['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['hs']); ?></td>
<td><?php echo round($result['weapons']['1']['extra']['hkp'],2); ?>%</td>
</tr>
I just can't wrap my head around how I would loop this? I have searched and looked at many articles, I just can't seem to get anything to work.
If you'd like to run through every result the easiest way would be to use foreach
e.g.
foreach($result['weapons'] as $tr){ ?>
<tr>
<td><?= strtoupper($tr['stat']['id']) ?></td>
<td><?= round($tr['extra']['accuracy'],2) ?>%</td>
//... etc.
</tr>
<?php } //...
If you'd prefer to define the amount of loops just use "for"
for ($i = 0; $i <= 10; $i++) { ?>
<tr>
<td><?= strtoupper($result['weapons'][$i]['stat']['id']) ?></td>
<td><?= round($result['weapons'][$i]['extra']['accuracy'],2) ?>%</td>
<td><?= round($result['weapons'][$i]['extra']['kpm'],2) ?></td>
//.... etc.
</tr>
<?php }
it's very easy to use foreach loop lets assume you got your value in variable $result_api then loop through this array
$i=0;
foreach($result_api as $result)
{ ?>
<tr>
<td><?php echo strtoupper($result['weapons'][$i]['stat']['id']); ?></td>
<td><?php echo round($result['weapons'][$i]['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons'][$i]['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['hs']); ?></td>
<td><?php echo round($result['weapons'][$i]['extra']['hkp'],2); ?>%</td>
</tr>
<?php
$i++;
}