I'm trying to output something special with the first row but it seems to output on every row, here is the code im using:
$query = mysql_query("SELECT * FROM chat WHERE id > '".$_GET["latest"]."' ORDER BY id DESC LIMIT 0, 20");
$number = mysql_num_rows($query);
$i = 1;
while ($row = mysql_fetch_assoc($query)) {
echo "<div class='babble' style='width:130px;overflow:hidden;margin:auto;'><font color=#ff4355><b>".$row['author']."</b></font><font color=#ff4355>:</font></b> ".$row['babble']."</div>";
if($i = 1)
{
echo "<script type='text/javascript'>newestid=".$row['id']."</script>";
$i = 2;
}
}
You need to use the comparison operator ==, not the assignment operator = in your if statement.
if ($i == 1)
Related
i want to show Random Item with Select by WHERE code = $Code
But if he finds like 10 Items, i want to show only 1 by 1. All 10 must show in a Row, Random Order with a 5 seconds timer. The Problem is, i dont will show the same Item again and again. If the 10 Items are done, he must look again to database and start again
I already tried to do it with putting all in array but my skills are low :(
Thats my try...
$result = $db->query( 'SELECT * FROM ads WHERE zipcode = "'.$zipcode.'";');
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
$count = mysqli_num_rows($result);
for($i = 0; $i < $count; $i++) {
$random = rand(0, $count);
$count -= 1;
$result[] = $data[$random];
echo json_encode($data);
unset($data[$random]);
}
I have the following code - it produces a series of queries that are sent to a database:
$a = 'q';
$aa = 1;
$r = "$a$aa";
$q = 54;
while($aa <= $q){
$query .= "SELECT COUNT(". $r .") as Responses FROM tresults;";
$aa = $aa + 1;
$r = "$a$aa";
}
The issue I have is simple, within the database, the number is not sequential.
I have fields that go from q1 to q13 but then goes q14a, q14b, q14c, q14d and q14e and then from q15 to q54.
I've looked at continue but that's more for skipping iterations and hasn't helped me.
I'm struggling to adapt the above code to handle this non-sequential situation. Any ideas and suggestions welcomed.
I have fields that go from q1 to q13 but then goes q14a, q14b, q14c, q14d and q14e and then from q15 to q54.
for($i=1; $i<=54; ++$i) {
if($i != 14) {
echo 'q' . $i . "<br>";
}
else {
for($j='a'; $j<='e'; ++$j) {
echo 'q14' . $j . "<br>";
}
}
}
If you don’t need to execute the statements in order of numbering, then you could also just skip one in the first loop if the counter is 14, and then have a second loop (not nested into the first one), that does the q14s afterwards.
You could get the columns from the table and test to see if they start with q (or use a preg_match):
$result = query("DESCRIBE tresults");
while($row = fetch($result)) {
if(strpos($row['Field'], 'q') === 0) {
$query .= "SELECT COUNT(". $r .") as Responses FROM tresults;";
}
}
Or build the columns array and use it:
$columns = array('q1', 'q2', 'q54'); //etc...
foreach($columns as $r) {
$query .= "SELECT COUNT(". $r .") as Responses FROM tresults;";
}
I am using following code
$con=mysql_connect('localhost','admin',"password");
$db=mysql_select_db('DbName',$con);
$query="SELECT COUNT(shuffel.clientid) as total_users FROM shuffel";
$result=mysql_query($query);
$total=mysql_fetch_array($result);
$total =$total['total_users'];
if($total>=2)
{
$result=$total/2;
$length =round($result);
if ($length > 10)
$length = 10;
for($i = 0; $i < $length; $i++)
{
$data = "SELECT * FROM shuffel ORDER BY RAND() LIMIT 2";
$shuffeluser = mysql_query($data);
int count = 0;
while($row = mysql_fetch_assoc($shuffeluser))
{
$my_array[] = $row;
count ++;
}
}
Now i want to get something like
$firstUserId = $my_array[0]->id;
However Everhthing just returns empty.
What is wrong in above code
mysql_fetch_assoc() returns array, not object so go get id you should use $my_array[0]['id'].
Also think about moving to PDO or mysqli_* because mysql_* is deprecated.
I didn´t get an answer yesterday on this question. https://stackoverflow.com/questions/15206351/jquery-slider-with-newest-content-first-gets-reversed-thumbnails-clicking-on-fi
So I tried for a simpler solution. I want to change this code:
$news_countpp = 6;
$result = mysqli_query($con,"SELECT * FROM tbl_news ORDER BY id DESC LIMIT ".($news_page * $news_countpp).", $news_countpp");
while($row = mysqli_fetch_array($result))
{
echo "<div><a href='#".$row['id']."' class='cross-link'><img src='/assets/images/news/news_".$row['id']."_slider_thumb.jpg' alt='".$row['news_img_alt']."' class='nav-thumb' alt='temp-thumb' style='width:60px; height:40px;'></a></div>";
}
The Part <a href='#".$row['id']." would echo descending numbers from #6 - #1. (newest news hast highest ID)
I want the numbers to be ascending instead (not according to the id) than it works.
I tried the following array instead of ".row['id'].":
$thumblink = array ('1', '2', '3', '4', '5', '6');
for ($i = 0; $i < 6; $i++) {
echo $thumblink[$i];
}
But this gives each of my 6 thumbnails the href "123456" instead of ascending numbers from 1 - 6 for the individual thumbnails. Do you know what I do wrong?
$news_countpp = 6;
$result = mysqli_query($con,"SELECT * FROM tbl_news ORDER BY id DESC LIMIT ".($news_page * $news_countpp).", $news_countpp");
$i = 1
while($row = mysqli_fetch_array($result))
{
echo "<div><a href='#".$i."' class='cross-link'><img src='/assets/images/news/news_".$row['id']."_slider_thumb.jpg' alt='".$row['news_img_alt']."' class='nav-thumb' alt='temp-thumb' style='width:60px; height:40px;'></a></div>";
$i++;
}
I would guess you're iterating over the entire array for every iteration of the while loop. Instead, initialise the array and a counter variable to 0 prior to the while loop, and only access one element per iteration (incrementing the counter by one as you do so).
How about something like this?
$i = 0;
while($row = mysqli_fetch_array($result)) {
$i++;
echo
"<div>",
"<a href='#".$row['id']."' class='cross-link'>",
"<img src='/assets/images/news/news_", $row[$i], "_slider_thumb.jpg'
alt='".$row['news_img_alt']."'
class='nav-thumb' alt='temp-thumb'
style='width:60px; height:40px;'>",
"</a>",
"</div>";
}
I'm querying a database for names that are numbered 1-26 alphabetically. I have the following code, but since HTML is structured tr then td, the table appears alphabetically by row as opposed to by column. How can I make it appear in order by column?
$query = mysql_query("SELECT name FROM people WHERE main=1 ORDER BY id");
$i = 0;
while($result = mysql_fetch_array($query)) {
$name = $result['name'];
if ($i % 5 == 0) echo "<tr>\n";
echo "<td width=\"150\">";
echo "".$name."<br />";
echo "</td>\n";
$i++;
if ($i % 5 == 0) echo "</tr>\n";
};
alpha beta charlie
delta echo foxtrot
vs.
alpha charlie echo
beta delta foxtrot
Also, I'm open to reworking the code if there's a more efficient way.
You could just access the output array in strides. Compute how many rows you need as the number of results divided by 5, and use the row count as the stride.
$ncols = 5;
$nrows = $nresults / $ncols + ($nresults % $ncols == 0 ? 0 : 1);
for ($i = 0; $i < $nrows; $i++)
{
// start row
for ($j = 0; $k < $ncols; $j++)
{
// print $results[$nrows * $j + $i]
}
// end row
}
You'll have to transfer your query results into an array $results first. Since you'll have to know the total number of results, this is sort of mandatory, though I'd be curious if anyone has a solution that can work while fetching the results.
Update: See Justin's answer for a cool solution that grows the output while fetching the query results line by line. Since it's currently being worked on, here's a summary (credits to Justin):
$nresults = mysql_num_rows($query);
$ncols = 5;
$nrows = (int) ceil($nresults / $ncols);
$i = 0; $cols = array_fill(0, $nrows, "");
while ($result = mysql_fetch_array($query))
$cols[$i++ % $nrows] .= "<td>$result['name']</td>";
echo "<tr>" . implode("</tr><tr>", $cols) . "</tr>";
Edit:
After the discussion in the comments between myself, Kerrek SB and the OP bswinnerton, the following code seems to be the most effective:
$columns = 3;
$rowcount = mysql_num_rows($query);
$rows = ceil($rowcount / $columns);
$rowdata = array_fill(0, $rows, "");
$ctr = 0;
while ($result = mysql_fetch_array($query))
$rowdata[$ctr++ % $rows] .= '<td>'.$result['name'].'</td>';
echo '<tr>'.implode('</tr><tr>',$rowdata).'</tr>';
This will create three columns, filled vertically (my original answer would create three rows). It also properly initializes the array (preventing PHP warnings), yields a correct row count for result counts that aren't divisible by the column count, and incorporates Kerrek's clever "calc-row-in-the-subscript" trick.
Original Post:
You could use arrays and implode() This way, you only have to make one pass through your results:
$row = 0;
$rows = 3;
$rowdata = array();
while($result = mysql_fetch_array($query))
{
if ($row >= $rows) $row = 0;
$rowdata[$row++] .= '<td>'.$result['name'].'</td>';
}
echo '<tr>'.implode('</tr><tr>',$rowdata).'</tr>';