Start a new column when MySQL result row count is >= 10 [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I cannot figure out how to get my code to create a new HTML column for results, in my table, in a while ($row = mysql_fetch_array($result)) { - when the results returned reach 10, I want a new table column to be created in the same HTML table.
How can I do this? The code I'm using is:
while ($row = mysql_fetch_array
($result, MYSQL_ASSOC)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
echo '<tr><td align="left" bgcolor=' . $row_color . '> <b>' . $row['manufacturer'] . '</b>: <a href=view_inventory.php?mdl_key=' . $row['mdl_key'] . '&man_key=' . $row['man_key'] . '&cls_key=' . $row['cls_key'] . '&sub_cls_key=' . $row['sub_cls_key'] . '> ' . $row['model'] . '</a></b></td></tr>';
$row_count++;
}

You should really use the new PDO interface as the mysql extension is deprecated long ago, read this documentation if you have a choice.
Still, with the old extension, simply do this:
$rowCount = mysql_num_rows($result);
if ($rowCount >= 10) {
while ($row = mysql_fetch_array($result)) {
// Do your extra column stuff here.
}
}
else {
while ($row = mysql_fetch_array($result)) {
// Do your normal stuff here.
}
}
// It is a good practise to remove variables after
// loops, this helps releasing memory in large scripts.
unset($rowCount, $row, $result);

Related

Accessing individual parts of php array [closed]

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

PHP query not showing all results in html table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
So I have the following query to show all users from my database:
<ul class="names">
<table>
<tr>
<th>Navn</th>
<th>Email</th>
<th>Score</th>
</tr>
<?php
$connection = mysql_connect('localhost', 'users', 'password'); //The Blank string is the password
mysql_select_db('users');
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$row=mysql_fetch_array($result);
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['iUserName'] . "</td><td>" . $row['iUserEmail'] . "</td><td>" . $row['iUserCash'] . " DKK</td></tr>"; //$row['index'] the index here is a field name
}
mysql_close(); //Make sure to close out the database connection
?>
</table>
</ul>
But for some reason it doesnt show all the data.
In only shows the user with iUserId 2 and not one.
Does anyone have an idea of what might be wrong?
I can log in with iUserId 1's credentials, and it shows fine the info on the login page.
But not here :S
Remove line $row = mysql_fetch_array($result);
Because this line starts to fetching records from your query results. First fetched record is record with id 1 and you do nothing with it.
Then you start echoing other records, but record with id 1 is already skipped.
You fetch the first row before your while loop is defined; once you enter the while loop it fetches the next row, which is the second. Remove the first mysql_fetch_array($result) operation.
Incidentally, also, the original mysql api in PHP is deprecated, it is recommended to use mysqli instead.
I think you should put it this way though
<?php
$connection = mysql_connect('localhost', 'users', 'password'); //The Blank string is the password
mysql_select_db('users');
let all the connection be outside the "ul" tag.
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$row=mysql_fetch_array($result);
?>
<ul class="names">
<table>
<tr>
<th>Navn</th>
<th>Email</th>
<th>Score</th>
</tr>
<?php
while($row){ //Creates a loop to loop through results
echo "<tr><td>" . $row['iUserName'] . "</td><td>" . $row['iUserEmail'] . "</td><td>" . $row['iUserCash'] . " DKK</td></tr>"; //$row['index'] the index here is a field name
}
mysql_close(); //Make sure to close out the database connection
?>
</table>
</ul>

php page not showing anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
im trying to create a drop down many and populate the options from fields in a database that i have.
So far i have got the following code:
<?php
session_start();
include_once("config.php");
$query = "SELECT Category FROM books";
$result = mysqli_query ($mysqli, $query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value=$row[Category]>$row[Category]</option>";
}
echo "</select>";
?>
</div>
</body>
</html>
However when i try to view the page nothing seems to happen.
My page remains blank and all i see is the color that i gave to the body in the css file.
Was wondering if anyone knew wh this was happening and if they can sort it out?
Thanks!
Assuming you are getting results from your query. Can find out by print_r($result). Think you are getting death screen because of quotes and missing opening for first option in dropdown.
echo '<select name="dropdown" value=""><option value="">Dropdown</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="' . $row['Category'] . '">' . $row['Category'] . '"</option>';
}
echo "</select>";

mysql_fetch_array, not valid - combining tables in one php page [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i've got the following code
$data = mysql_query(
"SELECT md.*, qr.*
FROM moduleDetails md
LEFT JOIN qResponses qr
ON qr.userno = md.userno");
print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
print "<tr>";
print "<th>Faculty</th> <td>".$info['faculty'] . "</td>";
print "<th>Module Code</th> <td>".$info['moduleCode'] . "</td>";
print "<th>Date Started</th> <td>".$info['dateStarted'] . "</td>";
print "<th>Module Title</th> <td>".$info['moduleTitle'] . "</td>";
print "<th>School</th> <td>".$info['school'] . "</td></tr>";
}
print "</table>";
it's giving me a error on line 31, which is the $info = mysql_fetch.... etc
What have i done wrong?.. i can't see a fix for this, it wasnt working fine before before i involved two tables.. any help would be great - cant see whats wrong - new to joining tables.
Probably you get no results from the query. Execute the query to verify it gives result and check you have results before trying to fetch them
if (mysql_num_rows($data) == 0)
die("No Records");
elseif (mysql_num_rows($data) > 0)
{
while($info = mysql_fetch_array( $data ))
{
...............
}
}
else { die("Something else went wrong"); }
Or even better wrap it in a try/catch

How to update a record based on if previous fields are occupied? [closed]

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 9 years ago.
Improve this question
Is there something wrong with my syntax its not executing in my php script? I am not updating all the records i am trying to update where invoice_no is equal to the '$id' from another form but only if pp1_dt, pp1_amt, pp1_ref is empty else move on to pp2_dt,pp2_amt,pp2_ref and so on to 5.
$i=1;
while($i <= 5) {
$pp_sql = "UPDATE Invoices SET pp'$i'_dt = '$pp1_dt', pp'$i'_amt = '$pp1_amt', pp'$i'_ref = '$pp1_ref' where invoice_no='$id' AND (coalesce(pp'$i'_dt, pp'$i'_amt, pp'$i'_ref) is null)";
if($db->exec($pp_sql)) {
$p_num = $i;
}
else {
$i++;
}
}
you can't use your counter variable inside your string, you need to concatenate it
$pp_sql = "UPDATE Invoices SET pp" . $i . "_dt = '$pp1_dt', pp" . $i . "_amt = '$pp1_amt', pp" . $i . "_ref = '$pp1_ref' where invoice_no=" . $id . " AND (coalesce(pp" . $i . "_dt, pp" . $i . "_amt, pp" . $i . "_ref) is null)";
the way you have it written now, it's trying to find columns in your table called pp$i rather than pp1, pp2, etc, which don't exist.
Shenir, if you want to update any record than you should try to use UPDATE instead of INSERT. if you want INSERT & UPDATE with same query than use key for duplicate records.
I suggest you should read this article http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html

Categories