I need to add a counter to the loop as well as pulling contents. What is the syntax for adding the "and for ($ColCount = ColCount + 1)"
<?php $ColCount = 0?>
<!--get artwork thumbnails -->
<?php
$dbname = 'pdartist2';
$table = 'artwork';
// query
$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '$SCID' ") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
foreach($row as $cell)
and for ($ColCount = ColCount + 1)
echo "$ColCount", "$cell";
}
mysql_free_result($result);
?>
I have no idea if that's valid in PHP. Maybe all it is is a forgotten $ sign before ColCount. But you could just do it the normal way:
foreach($row as $cell) {
$ColCount++;
echo $ColCount . $cell;
}
$ColCount = 0;
Code:
<!--get artwork thumbnails -->
<?php
$dbname = 'pdartist2';
$table = 'artwork';
// query
$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '$SCID' ") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
$colCount = 0;
foreach($row as $cell) {
$colCount++;
echo "$ColCount", "$cell";
}
}
mysql_free_result($result);
?>
As Bruce said, you could just do $ColCount++, But also you could just count the results with mysql_num_rows
$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '$SCID' ") or die(mysql_error());
$numRows = mysql_num_rows($result);
Shai
You are missing a semi-colon:
<?php $ColCount = 0?>
Should be:
<?php $ColCount = 0; ?>
And:
while($row = mysql_fetch_row($result)) {
foreach($row as $cell) {
$ColCount++; // adds 1 to colcount
echo $ColCount.$cell; // no need for quotes around variables. Also string concatination in PHP is using s dot (.)
}
}
mysql_free_result($result);
What i'll do is use an array like that
<!--get artwork thumbnails -->
<?php
$dbname = 'pdartist2';
$table = 'artwork';
# Define array
$Count = array();
// query
$result = mysql_query('SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '.$SCID) or die(mysql_error());
while($row = mysql_fetch_row($result)){
foreach($row as $cell){
$Count[$Cell]['Count'] = $Count[$Cell]['Count']++;
}
echo $ColCount.', '.$cell;
}
mysql_free_result($result);
So you have the count for each colomn but what I suggest is to use the mysql_num_rows(); like that :
$Query = mysql_query...
$Count = mysql_num_rows($Query);
$Count will be a number ;)
Then if($Count > 0)
If you're trying to count the number of columns in each row, then
while($row = mysql_fetch_row($result)) {
$colCount += count($row);
}
though I fail to see point of this. an SQL result set is a perfectly rectangular array. Variable number of rows, but every row will ALWAYS have the same number of fields as all the other rows in the result set. You'd only have to count the columns ONCE and cache that value.
Related
I am getting data from a MySQL database using an array (via PHP).
I was wondering, is it okay to subtract array values, like this (on line 7):
$num = 30;
$result1 = mysql_query('SELECT * FROM table1');
$result2 = mysql_query('SELECT * FROM table2');
while($row1 = mysql_fetch_array($result1) && $row2 = mysql_fetch_array($result2)) {
$sub = $row1['number'] - $row2['number'];
if($sub<=$num) {
echo $row1['person'];
}
I'm actually not getting any results back (just blank). So I was wondering if that line or any parts of my code is logically correct?
Try like this
$num = 30;
$result = mysql_query('SELECT number.table1 as n1, number.table2 as n2, colName.tableName FROM table1 JOIN table2 ON id.table1 = table1_id.table2');
while($row = mysql_fetch_array($result)) {
$sub = $row['n1'] - $row['n2'];
if($sub<=$num) {
echo $row['person'];
}
}
If I have an array $MovieDetails = array(); and it is populated by the query below with a foreach loop (5 elements; id, movie_year, genre, image, movie_name), how do I add another element (movie_rating) to the end of the array
$AllMovies = $con ->query
("
SELECT id, movie_year, genre, image, movie_name FROM movies;
");
while($row = $AllMovies->fetch_object()) {
$MovieDetails[] = $row;
}
Add movie rating into $row.
If you work with that as object, it's $row->movie_rating = 1.5
while($row = $AllMovies->fetch_object()) {
$row->movie_rating = 1.5;
$MovieDetails[] = $row;
}
If you work with that as array, use fetch_assoc() and $row['movie_rating'] = 1.5
while($row = $AllMovies->fetch_assoc()) {
$row['movie_rating'] = 1.5;
$MovieDetails[] = $row;
}
This way your row is an object
$AllMovies = $con->query("SELECT id, movie_year, genre, image, movie_name FROM movies;");
while($row = $AllMovies->fetch_object()) {
$row->movie_rating = 'movieRating';
$MovieDetails[] = $row;
}
If you want each row to be array, you should do:
while($row = $AllMovies->fetch_array()) {
$row['movie_rating'] = 'movieRating';
$MovieDetails[] = $row;
}
$MovieDetails['movie_rating'] = $movie_rating;
I'm working on coding a design for a project and have spent the past few days trying to get this to work but cannot wrap my head around how to do it.
I'm trying to get the first 4 results from a database, and print them to the header. For each result, the nav list number needs to increase by one. For example:
<li class="nav_list" id="nav_list1">RESULT 1
<li class="nav_list" id="nav_list2">RESULT 2
Heres what I have so far, but have no idea how I could get it to display correctly. Any help is appreciated!
$query = "SELECT id, title, description, groupid FROM category LIMIT 0, 4";
$result = mysqli_query($dbc, $query)
or die('Error querying database');
while ($row = $result->fetch_row()){
$catID = $row[0];
$catTITLE = $row[1];
}
?>
<ul id="nav_line1">
<?php
for ($i = 1; $i <= 4; $i++) {
print('<li class="nav_list" id="nav_list'.$i.'">catTITLEhere|</li>');
}
$i = 1;
while ($row = $result->fetch_row()) {
echo <<<EOL
<li id="nav_list{$i}"><a href="{$row[0]}"> etc...
EOL;
$i++;
}
Can you try this,
$query = "SELECT id, title, description, groupid FROM category ORDER BY id DESC LIMIT 0, 4";
$result = mysqli_query($dbc, $query)
or die('Error querying database');
echo '<ul id="nav_line1">';
while ($row = $result->fetch_row()){
$catID = $row[0];
$catTITLE = $row[1];
print('<li class="nav_list" id="nav_list'.$catID.'">'.$catTITLE.'|</li>');
}
echo "</ul>";
Try somethin like this:
query = "SELECT id, title, description, groupid FROM category LIMIT 0, 4";
$result = mysqli_query($dbc, $query)
or die('Error querying database');
$i = 1
while ($row = $result->fetch_row()){
$catID = $row[0];
$catTITLE = $row[1];
?><ul id="nav_line1"><?php
print('<li class="nav_list" id="nav_list'.$i.'">'.$catTITLE.'|</li>');
$i++
}
We have this code:
$rowArray;
$rowID = 1;
$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
$rowArray[$rowID] = $row['idCentros'];
$rowID = $rowID +1;
}
$numrows returns 4 (the rows we have in that table)...but for an unkown reason the loop starts retrieving the 2º row next it retrieves the 4º row and then it ends the loop ($row =false). As we understand this is generic code and the table definition is like this:
column idcentros int(11) pk notnull autoincremental
column nombre mediumtext
What could be happening? Thanks in advance...
try this:
$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);
$rowArray = array();
while($row = mysql_fetch_array($result))
{
array_push($rowArray,$row);
}
I don't see why the above code shouldn't work, but ... here's how I would do it:
$rowArray = array();
$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);
while($row = mysql_fetch_row($result)){
$rowArray[] = $row[0];
}
... I believe you have $rowID set to 1 just for visualisation later, but it's pointless - you should use HTML lists or some $counter++ variable for the output.
I am trying to get the qunt with are int and add them all up and show a total
$qunt = 0;
$result = mysql_query("SELECT *
FROM properties_items
WHERE user_propid='$view'
ORDER BY id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$itemid = $row['itemid'];
$qunt = $row['qunt'];
$qunt++;
}
echo $qunt;
$qunt = 0;
$result = mysql_query("SELECT *
FROM properties_items
WHERE user_propid='$view'
ORDER BY id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$itemid = $row['itemid'];
$qunt += $row['qunt'];
}
echo $qunt;
Dare I say that you probably aren't using the itemid, in which case there's no point in looping through a result set in code. Instead, try something like this:
$qunt = mysql_query "SELECT SUM(qunt) FROM properties_items WHERE user_propid='$view'";
$qunt += $row['qunt'];
And get rid of the ++ line.