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.
Related
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>";
i just want data will show if ['stt']==1
Here is the code :
<?php do { ?>
<tr>
<td><?php echo $row_daftaruser['id']; ?></td>
<td><?php echo $row_daftaruser['nama']; ?></td>
<td><?php echo $row_daftaruser['email']; ?></td>
<td><?php echo $row_daftaruser['username']; ?></td>
<td><?php echo $row_daftaruser['password']; ?></td>
<td><?php echo $row_daftaruser['alamat']; ?></td>
<td><?php echo $row_daftaruser['tgl_lahir']; ?></td>
<td><?php echo $row_daftaruser['stt']; ?></td>
</tr>
<?php } while ($row_daftaruser = mysql_fetch_assoc($daftaruser)); ?>
Can somebody help me?
you could try this:
<?php do { ?>
<?php if($row_daftaruser['stt'] == 1): ?>
<tr>
<td><?php echo $row_daftaruser['id']; ?></td>
<td><?php echo $row_daftaruser['nama']; ?></td>
<td><?php echo $row_daftaruser['email']; ?></td>
<td><?php echo $row_daftaruser['username']; ?></td>
<td><?php echo $row_daftaruser['password']; ?></td>
<td><?php echo $row_daftaruser['alamat']; ?></td>
<td><?php echo $row_daftaruser['tgl_lahir']; ?></td>
<td><?php echo $row_daftaruser['stt']; ?></td>
</tr>
<?php endif ?>
<?php } while ($row_daftaruser = mysqli_fetch_assoc($daftaruser)); ?>
This code will only display values if stt is equal to 1.
Also, use mysqli not mysql because it mysql is obsolete.
Why do not you write the query this way?
SELECT * FROM TABLE WHERE stt = 1;
I'm using Materialize css on my project, i have a table that shows mysql field called "status" and in this table i want change the color of the row if i change the "status" like "1=blue, 2=red..." Someone here knows how i can make a function to do this? Thank you.
table extample:
table class="striped bordered responsive-table">
<thead>
<tr>
<th>ID</th>
<th>Cliente</th>
<th>Objeto</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr>
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
</tbody>
One simple way would be:
<?php
$colorMap = [
1 => 'blue',
2 => 'red',
// add more
];
while ($row_os = mysqli_fetch_assoc($result_user)) { ?>
<tr style="background:<?php echo $colorMap[$row_os['status']] ?>">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
Of course you could also add a class depending on status the same way and do the styling in CSS.
Simple as Doing this on the row , only if the status must contain a value of either 1 or 2
<?php
while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr class="<?php echo $row_os["status"]==1?'blue':'red'?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
or if status varies (then follow #colburton answers ) or this
<?php
while($row_os = mysqli_fetch_assoc($result_user)){
$color="";
switch($row_os["status"]){
case 1:
$color="blue";
break;
case 2:
$color="red";
break;
//and so on
}
?>
<tr class="<?php echo $color;?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
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++;
}