Array seems to stop after 7 items - php

I have this odd behavior from a table I am drawing. When I add more than 7 entries to the DB for the table to draw, it seems like it crashes and doesn't draw anything. 7 or less and it works just fine.
I have no idea why it would be doing this???
$strSQL = "
SELECT u.username playername
, IFNULL(a.avatar,'default.jpg') playeravatar
FROM add_tournament_repeat_guests p
JOIN nfojm_users u
ON p.guestID = u.id
LEFT
JOIN nfojm_comprofiler a
ON u.id = a.user_id
WHERE p.parent_id = $tournID
";
$query = mysqli_query($con, $strSQL);
while ($row = mysqli_fetch_array($query)) {
if (!in_array($row["playername"], $playernames)) {
$i=0;
$indexes[$row["playername"]] = 0;
$playernames[] = $row["playername"];
} else {
$indexes[$row["playername"]]++;
$i = $indexes[$row["playername"]];
}
if (!in_array($row["playeravatar"], $playeravatars)) {
$i=0;
$indexes[$row["playeravatar"]] = 0;
$playeravatars[] = $row["playeravatar"];
} else {
$indexes[$row["playeravatar"]]++;
$i = $indexes[$row["playeravatar"]];
}
}
print('<table class="block" style="border:2px solid #999999; width:175px; background-color: aliceblue; margin-top:-7px"><thead><tr><th style="color:white; background-color:#444444; height:35px;"> <h2>Leaderboard</h2> </th></tr></thead><tbody>');
foreach (array_combine($playeravatars, $playernames) as $playeravatar => $playername) {
print("<tr><td style='padding-left:8px;' class='block'><h4><img src='http://www.arcadeicons.com/images/comprofiler/" . $playeravatar . "' height='35' width='35' style='border-radius:50%'> " . $playername . "</h4></td></tr>");
}

$playeravatars and $playernames have different lengths, that's why array_combine() returns FALSE instead of combined array as you expect (php.net/array_combine)
2 users with different names and without avatars will generate 2 rows in $playernames, but only 1 row in $playeravatars -- that is the reason of length mismatching
You may write your code simplier:
echo '<table class="block" style="border:2px solid #999999; width:175px; background-color: aliceblue; margin-top:-7px"><thead><tr><th style="color:white; background-color:#444444; height:35px;"> <h2>Leaderboard</h2> </th></tr></thead><tbody>');
while ($row = mysqli_fetch_array($query)) {
echo "<tr><td style='padding-left:8px;' class='block'><h4><img src='http://www.arcadeicons.com/images/comprofiler/" . $row['playeravatar'] . "' height='35' width='35' style='border-radius:50%'> " . $row['playername'] . "</h4></td></tr>";
}

Related

PHP in_array is not finding value that is there

I have this code:
$sql_zt = "SELECT
inh_pr.extra_bew_zetten AS extra_bew_zetten
FROM 5_offerte_id AS off
LEFT JOIN 6_offerte_inh AS off_inh
ON off_inh.offerte_id = off.id
LEFT JOIN 3_product_folder AS fld
ON fld.folder_id = off_inh.folder_id
LEFT JOIN 0_calculatie_inh_id_geg_lntk_product AS inh_pr
ON inh_pr.calculatie_inh_id = fld.product_id
WHERE off.dossier_id = ".$row['id']." AND inh_pr.extra_bew_zetten = 'ja' AND off.offerte_nr = (SELECT MAX(offerte_nr) FROM 5_offerte_id WHERE dossier_id = ".$row['id'].") ";
if(!$res_zt = mysql_query($sql_zt,$con))
{
include('includes/errors/database_error.php');
}
if(mysql_num_rows($res_zt) > 0)
{
if(in_array('ja', mysql_fetch_array($res_zt)))
{
echo '<img border="0" src="images/icon/zetten.png" title="'.$lang['zetten'].'"> ';
}
}
This is my output example with the query:
Value 'ja' is not found with in_array. What might be the problem?
there are more than one record you can use loop for it
while($row = mysql_fetch_array($res_zt)){
if(in_array('ja',$row))
{
echo '<img border="0" src="images/icon/zetten.png" title="'.$lang['zetten'].'"> ';
}
}

How can I use a FOREACH loop inside of a WHILE loop to create several columns

PROBLEM
I am pulling data from my database...using a WHILE loop & I want the returned data to be coupled into "scalable in size" groups.
I thought that by using a FOREACH LOOP inside of the while loop, I could accomplish this, apparently I thought wrong..ARRG!
LINK
View problem here: http://sis-cr.com/NEWSTORE/store.php?cur_str=cel
CODE
$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {
$cur = 0;
$rowNum = 1;
foreach($list_of_xtras as $key => $value){
if($cur == 0){
echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">';
}
echo ' <li style="list-style:none;">' . $value. '</li>';
if($cur == 2)
{
echo '</ul>';
$cur = 0;
$rowNum++;
}
else
{
$cur++;
}
}
}
After 3 hours of trying to fix this, and at the risk of exposing my very obvious lack on understanding, I humbly bow before the collective minds of SO....and shout...HELP, I've coded and can't get up!
I might be off track. I think you want:
$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {
$rowNum = 1;
echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
for($curr=0; $curr<2; $curr++){
echo '\t<li style="list-style:none;">' . $list_of_xtras[$curr] . '</li>\r\n';
}
echo '</ul>';
$rowNum++;
}
Seems like you're just looking for 2 results, so I don't know if FOREACH is best here. Maybe just a FOR loop.
Edit, after I saw your comment. Sounds like you want to wrap 10 results in a UL. To do that it would be something like:
$rowNum = 1;
echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {
if($rowNum % 10 == 0){
echo '</ul><ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
}
echo '\t<li style="list-style:none;">' . $list_of_xtras[0] . '</li>\r\n';
$rowNum++;
}
echo "</ul>\r\n";

Loop through database for a particular record and show results - PHP

i'm looking for a way to loop through the database and shows results in a html page, i'm actually creating a sorta fake email system for a little project.
so i have a table name "SENDS" with 5 columns ID, Mailtext, Touser, Subject, Fromuser.
what i wanna do is when user log in system connects to the database and select everything
from SENDS where touser='$email' -$email is defined when user logs in- and display it in a html div
<div class="oubx">
<div class="inbx">
<span class="from"></span>
<span class="subject"></span>
<span class="mailtext"></span>
</div>
</div>
.oubx {
height:27px; width:800px;
border:1px solid black;
margin-top:;
margin-left:80px;
float:left;
background-color:white;
}
.inbx {
height:22px; width:700px;
border:1px solid black;
margin-top:1px;
margin-left:45px;
background-color:white;
font-family:sans-serif;
font-weight:bold;
font-size:16px;
color:#808080;
float:left;
}
from of course tells whos it from
subject is subject
mailtext is what the mail is (it's hidden of course jQ will take care of it once clicked)
i have used mysqli_num_rows, mysqli_fetch_array, for loops, foreach etc but i just can't seem to get my head around it i'm having a hard time figuring out how to do it setp by steps or what the step is. please help :) thanks.
$connectDB = mysqli_connect("host","root","","db");
$touser = mysqli_query($connectDB, "select * from sends where touser='$email'");
$to_numrows = mysqli_num_rows($touser);
$to_fetch = mysqli_fetch_array($touser);
//printing of html code = how many rows
$ib = '<div class="oubx"><div class="inbx"><span class="from">'.$value.'</span><span class="subject">'.$value.'</span></div></div>';
for ($i = 0; $i < $to_numrows; $i++) {
echo $ib;
}
the rest is work in progress..
Try this,
$connectDB = mysqli_connect("host","root","","db");
$touser = mysqli_query($connectDB, "select * from sends where touser = '$email'");
$all_rows = mysqli_fetch_all($touser, MYSQLI_ASSOC);
$ib = '';
foreach($all_rows as $row) {
$ib .= '<div class="oubx">'
. '<div class="inbx">'
. '<span class="from">' . $row['Fromuser'] . '</span>'
. '<span class="subject">' . $row['Subject'] . '</span>'
. '</div>'
. '</div>';
}
echo $ib;
1- mysqli_fetch_all will fetch all result row into a PHP array
2- We loop through it with foreach (no need to know the rows count)
3- Foreach row, we append the HTML to the $ib intially set to empty string.
Hope it helps.
put the query result in a variable:
while ($row = $touser->fetch_array()) {
$rows[] = $row;
}
and iterate it with a loop:
foreach ($rows as $row) {
/* show appropriate html here */
}

How to design the data that you gathered from the table

I've printed all the data from the database, but my main problem is how to design my data.
I have a table named post_tbl and columns(post_id,post_message,post_date)
this is my query:
$query = "SELECT `post_id`,`post_message` FROM `post_tbl` ORDER BY `post_date`;
This is how I print in php:
if($query_run = mysql_query($query))
{
while($query_row = mysql_fetch_assoc($query_run))
{
$ex_post_id = $query_row['post_id'];
$ex_post_message = $query_row['user_name'];
$ex_post_date= $query_row['post_date'];
echo $ex_post_message;
}
}
how do I make my ex_post_message have a unfirom border and width using html and css? pls help. thanks
if($query_run = mysql_query($query))
{
while($query_row = mysql_fetch_assoc($query_run))
{
$arrMaster[] = $ex_post_message;
}
}
foreach ($arrMaster as $key => $value)
{
if($i==0)
{
$table1.="<tr>";
foreach ($value as $keyc => $valuec)
{
$table1.="<th>".$keyc."</th>";
}
$table1.="</tr>";
$i=1;
}
$table1.="<tr>";
foreach ($value as $keyc => $valuec)
{
$table1.="<td>".$valuec."</td>";
}
$table1.="</tr>";
}
$table1 .= "</table>";
echo $table1;
at this way you can add any style to your table or any class
Yes you can echo HTML element like:
echo "<div class='classname1'>" . $ex_post_message . "</div>";
Then the class classname1 should handle the design. Like the following:
<style>
.classname1{
width: 100px;
height: 30px;
border: 1px solid blackl
}
</style>
There are many ways to achieve this. The one that I've provided is just an example.

applying stylesheet class for dynamic rows

I am trying to apply css class for dynamic rows in php. But its not happening. Here is my code
please someone suggest me where i am going wrong. I have declared a counter for rows but not getting where to increment its count.
<table width='100%' border='0' cellspacing='0' cellpadding='2'>
<tr>
<td class='tddash'>10 latest unpaid customer invoices</td>
</tr>
<tr>
<td valign='top'>";
$SQL = "SELECT salesorders.orderno,
debtorsmaster.name,
custbranch.brname,
salesorders.customerref,
salesorders.orddate,
salesorders.deliverydate,
salesorders.deliverto,
salesorders.printedpackingslip,
salesorders.poplaced,
SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)/currencies.rate) AS ordervalue
FROM salesorders INNER JOIN salesorderdetails
ON salesorders.orderno = salesorderdetails.orderno
INNER JOIN debtorsmaster
ON salesorders.debtorno = debtorsmaster.debtorno
INNER JOIN custbranch
ON debtorsmaster.debtorno = custbranch.debtorno
AND salesorders.branchcode = custbranch.branchcode
INNER JOIN currencies
ON debtorsmaster.currcode = currencies.currabrev
WHERE salesorderdetails.completed=0
GROUP BY salesorders.orderno,
debtorsmaster.name,
custbranch.brname,
salesorders.customerref,
salesorders.orddate,
salesorders.deliverydate,
salesorders.deliverto,
salesorders.printedpackingslip,
salesorders.poplaced
ORDER BY salesorders.orderno";
$SalesOrdersResult1 = DB_query($SQL,$db);
echo "<table width='100%' celpadding='2' class='selection'><tbody>";
$TableHeader = "<tr><th> Customer </th><th>Order Date</th><th>Delivery Date</th><th>Delivery To</th><th>Order Total</th></tr> ";
$k = 0;
while ($row = DB_fetch_array($SalesOrdersResult1))
{
if ($k == 1){
echo '<tr class="EvenTableRows">';
$k = 0;
} else {
echo '<tr class="OddTableRows">';
$k = 1;
}
$FormatedOrderValue1 = locale_number_format($row['ordervalue'],$row['currdecimalplaces']);
//$TotalOrderValue = $array_sum($FormatedOrderValue1);
//$FormatedOrderValue1 = locale_number_format($myrow['ordervalue'],$_SESSION['CompanyRecord']['decimalplaces']);
$FormatedOrderDate = ConvertSQLDate($row['orddate']);
$FormatedDelDate = ConvertSQLDate($row['deliverydate']);
echo " <td> " . $row['name'] . " </td>";
echo " <td>$FormatedOrderDate</td><td>$FormatedDelDate</td><td> " . $row['deliverto'] . " </td><td>$FormatedOrderValue1</td> ";
}
//echo "<tr><td colspan='3'>Total</td><td colspan='2'>$TotalOrderValue</td></tr></tbody>";
//echo $array_sum($FormatedOrderValue1);
echo "</table>";
echo"</td>
</tr>
</table>
Try to put this inside your while loop.
if ($k == 1){
echo '<tr class="EvenTableRows">';
$k = 0;
} else {
echo '<tr class="OddTableRows">';
$k = 1;
}
If you want decorate your table, and you can use css use it. Don't use server side language
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
Three issues with your code (four if you count the fact that you have a massive amount of HTML that should be moved outside of being echo'ed within php)
1) You have no $k++; to increment your $k value
2) You have a "tr" within the $row['name'] line that needs to be removed.
3) You need to move the entire if($k) block INSIDE of your while() loop.

Categories