Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to echo 3rd column's value up to the last column
i know i can do this by echo $result[3]; echo $result[4]; echo $result[5]; and so on up to the last but is there any way to do it this way echo $result[3] UNTIL $result[last]; ?
please help me
thanks in advance
You need to return numeric indices from DB query and simple for loop. Notice the MYSQL_NUM flag added:
while($result=mysql_fetch_array($query, MYSQL_NUM)) {
for ($index = 3; $index < count($result); $index++) {
echo $result[$index];
}
}
EDIT:
An alternative. Use mysql_fetch_assoc and access your data using column names instead of numeric indices. This improves readablity of code as well.
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
while($row = mysqli_fetch_assoc($result) and ($counter < $max)){
$data[$row['order_id'] . $row['ship_to_name'] .
$row['shipping_address'] . $row['billing_address'].
$row['total_paid_incl_vat'] . $row['total_paid_excl_vat'].
$row['base_shipping_incl_tax']][] = $row['name'] . ' €' . $row['base_row_total_incl_tax'];
How can I access $row['order_id'] when looping through it like
foreach ($data as $group_title => $groups) {
When using echo $group_title; it just echoes out everything but I would like to access the single values.
Thanks!
Maybe what you are really looking to do is
while($row = mysqli_fetch_assoc($result) and ($counter < $max)){
// create or fix the $row['name']
$row['name'] = ' €' . $row['base_row_total_incl_tax'];
// add the row to the array indexed by `order_id`
$data[$row['order_id']] = $row;
}
Now you can
foreach ($data as $id => $theRow) {
echo "Order id = $id<br>";
echo "The Address is $theRow[shipping_address]<br>";
// etc
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to create a table that has dynamic rows () and in each rows has 5 cells (td) .
for example:
$rows=mysqli_affected_rows($dbCnn);
If for example $rows=7, we should have two tr. first tr has 5 td, and the second tr has two td.
...If for example $rows=7, we should have two tr. first tr has 5 td, and the second tr has two td.
Use for loop for this.
$rows=mysqli_affected_rows($dbCnn); // for example, $rows = 7;
$counter = 1;
echo "<table><tr>";
for($i = 0; $i < $rows;){
if($counter % 6 == 0){
echo "</tr><tr>";
}else{
echo "<td> YOUR_VALUE </td>";
++$i;
}
++$counter;
}
echo "</tr></table>";
Here's the demo
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to make a simple function that creates an HTML table that first gets the headers in th-tags and then dumps all the rows into html table rows.
Here's what I got so far.
function dumpMysqlTable($tblName) {
$result=mysql_query("SELECT * FROM `$tblName`");
echo "<table>";
echo "<tr>";
$i=0;
while($i<mysql_num_fields($result)) {
$fields=mysql_fetch_field($result, $i);
echo "<th>".$fields->name."</th>";
$i++;
}
echo "</tr>";
echo "<tr>";
$j=0;
while($j<mysql_num_rows($result)) {
$k=0;
while($row=mysql_fetch_array($result)) {
echo "<td>".$row[$k]."</td>";
$k++;
}
$j++;
}
echo "</tr>";
echo "</table>";
}
However, it messes up badly, and picks rows from the table its not supposed to and I just can't get it to work. Please help :)
You are making several mistakes.
while($row=mysql_fetch_array($result)) {
will return a whole row (or line) of the table. So $row[$k] references a row (one line in the table), not a cell value.
Then you should know that mysql_fetch_array by default will return an array with both numerical keys and associative indices. So you should either use mysql_fetch_array( $result, MYSQL_NUM ) or simply mysql_fetch_row(). Then you can loop through the row:
while( $row = mysql_fetch_row( $result ) ) {
echo "<tr>";
foreach( $row as $cell )
{
echo "<td>".$cell."</td>";
}
echo "</tr>";
}
Notice you also forgot to open a new tr for each line.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hello i am beginner to PHP.I am not getting an idea regarding how to get all the rows as an output in the form of anchor tags .Here is my following code where blogtable is the name of the table . For instance i have five rows in a table . I want the entire rows as links as a resultant.By executing the following code i am getting the records in a table but unable to transform these results into anchor tags.Kindly suggest me an idea to get each and every row in the form of anchor tag.Thanks in advance.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$data = mysql_query("select * from blogtable");
while ($col = mysql_fetch_field($data)) {
echo $col->name;
}
while ($row = mysql_fetch_row($data)) {
for ($i = 0; $i < count($row); $i++) {
echo"<br/>";
echo"$row[$i]></a>";
echo"<br/>";
}
}
?>
Just add your link inside the for loop:
echo "<a href='http://example.com'>" . $row[$i] . "</a>";
Try this: echo "".$row[$i]."";
echo"$row[$i]></a>;
This is not the correct syntax for an anchor tag. Try something like:
echo "<a href='some site'>$row[$i]</a>";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have fixed my earlier problem, but now the loop produces 8 or more images for each. In one gallery I have one a single image but get it eight times, not ten, not three, what's with the eight loops?
while($row = mysql_fetch_array($result)){
$AID = $row['AID'];
$ThumbFilePath = $row['ThumbFilePath'];
$Title = $row['Title'];
$DisplayOrder = $row['DisplayOrder'];
foreach($row as $cell)
{
echo "<div id='clear'></div>";
echo "<div id='thumb_container'>";
echo "<a href='gallery_detail.php?AID=$AID'><img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'></a>";
echo "<div id='name_spacer'></div>";
echo "<div id='thumbdesc'>";
echo "$Title";
echo "</div>";
echo "</div>";
}
}
You have a nested loop.
while($row = mysql_fetch_arry($result)){ //once for each row in the database
foreach($row as $cell){ //once for each field in the table
...
}
}
I'm guessing your table has 8 fields. Remove the foreach{}.
8 columns selected in $result
so $row is an array consist of 8 element.
foreach($row as $cell)
foreach will loop 8 times.