So I have a table with a value $i for a column that simply counts/labels each row (goes from 1 to 12).
I have an array that I would like to add, into another table column. What I am trying to do, is have the array change the value it is outputting each time the $i value changes.
This is sort of what it looks like as for coding:
<?php
$i=1;
$myarray= array('one', 'two', 'three', 'etc...')
?>
<center>
<?php
echo'<table width="100%">';
echo '<tr>';
echo '<th></th>';
echo '<th>Country</th>';
echo '<th>Greeting</th>';
echo '<th>Translation</th>';
echo '<th>Language</th>';
echo '</tr>';
$number_of_rows = 11;
for ($row = 0; $row <= $number_of_rows; $row++) {
echo '<tr>';
echo '<td >'. $i++ . '</td>';
echo '<td >'. “Blue” . '</td>';
echo '<td >'. “Red” . '</td>';
echo '<td >'. “Green” . '</td>';
echo '<td >'. “Black” . '</td>';
echo '</tr>';
}
?>
</center>
I'm quite new to php, so it's a bit confusing to me. Sorry for any mistakes.
This is where I'm a bit stuck. I want the row that says "Green" to be the row that I am going to implement my array. Each row it will say something different, as the $i value is different for each row. However, I'm not quite sure how to implement this with the current set up that I have going? Is there a way I can do it? Or do I have to take a different route?
Thanks.
Related
I've been struggling for weeks just to analyze the problem of my code, the JQuery Pagination table didn't show the PHP value. I thought it would be PHP that causes the problem.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>No.</th>
<th>Nama Mitra</th>
<th>Jenis Kelamin</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
//I think the problem starts here
while ($rowmitra = mysqli_fetch_array($mitra)) {
echo '<tr>';
echo "<th scope='row'>" . $i . "</th>";
echo "<td>" . $rowmitra['id_mitra'] . "</td>";
echo "<td>" . $i . "</td>";
echo "<td>" . $rowmitra['mitra'] . "</td>";
echo '<td><img width="200px" class="shadow-sm" src="image/logo/' . $rowmitra["logo"] . '"></td>';
echo '<td><button>EDIT</button></td>';
echo "</tr>";
$i = $i + 1;
}
?>
</tbody>
</table>
And here's what I do to fetch Mitra table
$mitra = $koneksi->query("SELECT * FROM `mitra`");
$rowmitra = mysqli_fetch_array($mitra);
I put it in a different file that I use include 'head.php'; command to merge both. When I fetch it in outside while command it works.
It didn't fetch an array of my PHP when I put it in while. Am I clumsy enough not to know where is the problem exactly? I've tried to match the variable and also search for alternative ways to fetch array. But it didn't work.
Thank you for your help.
You should not declare the result fetch array outside while loop. Remove this $rowmitra = mysqli_fetch_array($mitra); and try with mysqli_fetch_assoc to get database column name.
<?php
$mitra = $koneksi->query("SELECT * FROM `mitra`");
$i = 1;
//I think the problem starts here
if (mysqli_num_rows($mitra) > 0) {
while ($rowmitra = mysqli_fetch_assoc($mitra)) {
echo '<tr>';
echo "<th scope='row'>".$i."</th>";
echo "<td>".$rowmitra['id_mitra']."</td>";
echo "<td>".$i."</td>";
echo "<td>".$rowmitra['mitra']."</td>";
echo '<td><img width="200px" class="shadow-sm" src="image/logo/'.$rowmitra["logo"].'"></td>';
echo '<td><button>EDIT</button></td>';
echo "</tr>";
$i = $i + 1;
}
}
?>
This is my current code (and image showing output). I want the table to be aligned and the Names of the values keep repeating. How can I achieve this?
<?php
$country['ph'] = array('Philippines', 'Manila', '+8');
$country['ru'] = array('Russia', 'Moscow', '-4');
$ctr = 1;
foreach ($country as $key => $value) {
echo '<table border = "1" >';
echo '<td>No';
echo '<td>Flag';
echo '<td>Code';
echo '<td>Name';
echo '<td>Capital';
echo '<td>Time Zone';
echo '<tr>';
echo '<td>', $ctr++, '</td>';
echo "<td><img src = \"/csnclass/img/flags/$key.png\"/></td>";
echo "<td>$key</td>";
echo '<td>',$value[0], '</td>';
echo '<td>',$value[1], '</td>';
echo '<td>',$value[2], '</td>';
echo '</tr>';
echo '</table>';
}
?>
Presently, your foreach is outputting a <table> for each item it iterates through - so the table cells in these tables won't be lined up, as cells in different tables have no relation to each other. Similarly, you're outputting the header of each table column for every single item as well.
A quick way to fix this is to take some of the echo calls outside of the foreach loop (mainly relating to the table and headers):
// Start table and print out headers
echo '<table border = "1" >';
echo '<tr>';
echo '<td>No';
echo '<td>Flag';
echo '<td>Code';
echo '<td>Name';
echo '<td>Capital';
echo '<td>Time Zone';
echo '</tr>';
// Print data for each country
foreach ($country as $key => $value) {
echo '<tr>';
echo '<td>', $ctr++, '</td>';
echo "<td><img src = \"/csnclass/img/flags/$key.png\"/></td>";
echo "<td>$key</td>";
echo '<td>',$value[0], '</td>';
echo '<td>',$value[1], '</td>';
echo '<td>',$value[2], '</td>';
echo '</tr>';
}
// End table
echo '</table>';
Hope this helps! Let me know if you have any questions.
I have mysql table with following columns :
no, name, oname, quantity
I have n number of records in the tables ,I want to fetch these records in an html table that dynamically increases'decreases its row length according to the number of rows in mysql database. I am trying following but its not working.can anyone help me out here
<?php
include './connection.php';
$query = "select * from orderdetails";
$result = mysql_query($query);
echo '<table border="1" style="width:600px" align=center >';
echo '<tr bgcolor="lavendar">';
echo '<td width="15%">Order No.</td>';
echo '<td>Name</td>';
echo '<td>Order</td>';
echo '<td>Quantity</td>';
echo '</tr>';
echo '</table>';
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
echo '</table>';
?>
Where you are printing out your values, there are a few errors.
In this specific code block:
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
First off all, you would want to concatinate your strings, PHP uses . for that.
For example, to concatinate "World!" to "Hello, ", to print out Hello, World!, you would use the following:
echo "Hello, " . "World!";
You are also missing a variable sign ($) in your code, when printing out the rows.
In your case, you're using echo '<td>' row['no'] '</td>';,
where it should have been echo '<td>' . $row['no'] . '</td>';
To clarify, your code should look like this:
while($row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' . $row['no'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['oname'] . '</td>';
echo '<td>' . $row['quantity'] . '</td>';
echo '</tr>';
}
Relevant PHP documentation:
PHP String Concatination
Echo documentation
There's a typo-
row['no']
to
$row['no']
Similarly other variables too.
I want to retrieve XML data from soundcloud using a simple search engine, that retrieves results in a simple results table array
$tracks = "https://api.soundcloud.com/tracks?q=".$var."&client_id=bbba84be29098bdaad6a5de1c048e3e9&limit=10";
$mysongs = simplexml_load_file($tracks);
echo "<table>";
echo '<table cellpadding="0" cellspacing="0">';
echo '<tbody>';
echo '<tr>';
echo '<th>State</th> <th>Name</th><th>Song</th><th>artist</th><th>user</th> <th>user</th>';
echo '</tr>';
echo '</tbody>';
foreach ($mysongs->track->user as $track) {
echo '<tbody>';
echo '<tr>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo " ";
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo '</tr>';
echo '</tbody>';
}
this is my code but nothing seems to display as a result, i have tested the same format of code on another api (yelp) and it does work in xml, so what am i missing here? i dont want to use json, i need it to be xml ( the user -> kind ) is just for testing the code right now.
the variable $user doesn't exist. the variable you want is $track. so where you have
echo $user->kind
replace that with
echo $track->kind
you can see the format of the returned object (and it's hierarchy by running the code in the cli and
print_r($mysongs)
This is my table.
<?php
while(($result = mysqli_fetch_assoc($query))){
echo '<tr>';
echo '<td>';
echo $result['serial'];
echo '</td>';
echo '<td>';
echo $result['address'];
echo '</td>';
echo '<td>';
echo ''.$result['name'].'' ;
echo '</td>';
echo '<td>';
echo $result['postal'];
echo '</td>';
echo '<td>';
echo $result['website'];
echo '</td>';
echo '</tr>';
}
?>
Now I want that if a user moves his mouse over one row, the color should change.
If you don't need to support IE 6, add this to your CSS:
table tr:hover {
background:orange;
}
Make your <tr> tags <tr onMouseOver="this.bgColor='#EABF4E';">, or use table tr:hover in CSS.
I am a bit of a noob, but I think you need to give the table row a class, then give that class a hover property in your css file.
Add a class to your table row like so:
echo '<tr class="highlighter">';
You can name it anything, just make sure you use the same name in your css file.
Now, style the class so that it's color changes when a user's mouse hovers over it:
.highlighter:hover {
background: #ffff99;
}