PHP in_array is not finding value that is there - php

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'].'"> ';
}
}

Related

Append data and display html table

I have some data set. I want to arrange data like this:
My code displays result like this:
.
How can I fix this. This is my code:
<?php
echo "<tr><th></th><th>Control</th><th>Sub 1</th><th>Sub2</th></tr>";
$i=0;
$PrevIssoff=0;
$sql="SELECT `code`, `name`,`type`
FROM `testdate`
WHERE `code`='11111'
ORDER BY `code` ASC, `type` ASC ";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$frm=$row['code'];
if ($frm != $PrevIssoff && $row['type']==0)
{
$i=$i+1;
$PrevIssoff=$frm;
echo "<tr><td>$i</td><td>$row[name]</td><td></td><td></td></tr>";
}
else if($row['type']==1 )
{
echo "<tr><td></td><td></td><td>$row[name]</td><td></td></tr>";
}
else if ($row['type']==2)
{
echo "<tr><td></td><td></td><td></td><td>$row[name]</td></tr>";
}
}
echo"</table>";
?>
Database table structure - testdate
databasetable
First, your data needs to be normalized. There should be a way to write a database query to skip this step if you look hard enough.
Walkthrough each row in the while loop, building a close representation for it an array.
$dataTable = [];
while ($row = mysqli_fetch_array($result)) {
$numRows = count($dataTable);
$lastRowIndex = $numRows == 0 ? 0 : $numRows-1;
if($numRows == 0) {
$dataTable[] = [];
}
if(count($dataTable[$lastRowIndex]) == 3) {
$dataTable[] = [];
$lastRowIndex += 1;
}
$type = $row['type'];
$dataTable[$lastRowIndex][$type] = $row['name'];
}
Then make your $rowsMarkup which can be readily concatenated with the table heading.
$rowsMarkup = array_map(function($row) {
return '<tr>'.
'<td>'.$row[0] .'</td>'.
'<td>'.$row[1] .'</td>'.
'<td>'.$row[2] .'</td>'.
'</tr>';
},
$dataTable
);

Array seems to stop after 7 items

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>";
}

How do I get the correct information out of my objects/ array with php / mysql?

I'm not a expert in programming but I want to set my old website over to new design and herefor I want to re-use my old code (slightly adjusted).
With the following function I get all artist information and guitar songs written. To display in a table.
but the artist information (aArtist) is no problem but the song information is not working correctly. I think I'm not far away but what should/could i try to make it work.
When printing (var_dump) the $aArtist I do get all information, its now hot to manage this.
Thanks for all suggestions.
Jan
function getArtistBySeoName($sSeoName,$sSortColumn,$sSortDirection,$sWhere)
{
$sWhereAdditions = "";
if($sWhereAdditions == "" && $sWhere != "")
{
$sWhereAdditions = " WHERE ".$sWhere;
}
$sSeoName = str_replace("'", "", $sSeoName);
$sQuery = "SELECT * FROM artist a
LEFT JOIN artist_info ai ON a.artist_id = ai.artist_info_artist_id
WHERE artist_seoname = '".$sSeoName."'
".$sWhereAdditions." ";
$oQueryResult = query($sQuery);
$oArtist = $oQueryResult->fetch_assoc();
$aSongs = array();
$sQuery2 = "SELECT s.song_id,s.song_name,a.*,aa.*,st.*,ss.*,d.* FROM song s
LEFT JOIN song_type st ON s.song_song_type_id = st.song_type_id
LEFT JOIN song_style ss ON s.song_style_id = ss.song_style_id
LEFT JOIN song_status sst ON sst.song_id = s.song_id
LEFT JOIN artist_album aa ON s.song_album_id = aa.artist_album_id
LEFT JOIN download d ON s.song_id = d.download_song_id inner join artist a on a.artist_id=s.song_artist_id
WHERE s.song_artist_id = ".$oArtist[artist_id]." and (sst.status=1 or sst.status=2 or sst.status=3) ORDER BY ".$sSortColumn." ".$sSortDirection;
$oQueryResult2 = query($sQuery2);
$i=0;
while($oSong = $oQueryResult2->fetch_assoc())
{
$i++;
$aSongs[]=$oSong;
}
$oArtist[songs] = $aSongs;
$aAlbums = array();
$sQuery3 = "SELECT * FROM artist_album aa
INNER JOIN song s ON s.song_album_id = aa.artist_album_id
WHERE s.song_artist_id = ".$oArtist[artist_id]." GROUP BY aa.artist_album_id";
$oQueryResult3 = query($sQuery3);
while($oAlbum = $oQueryResult3->fetch_assoc())
{
$aAlbums[]=$oAlbum;
}
$oArtist[albums] = $aAlbums;
$aImages = array();
$sQuery3 = "SELECT * FROM artist_images ai
WHERE ai.artist_image_artist_id = ".$oArtist[artist_id]." ";
$oQueryResult3 = query($sQuery3);
while($oImage = $oQueryResult3->fetch_assoc())
{
$aImages[]=$oImage;
}
$oArtist[images] = $aImages;
$oArtist[songscount] = $i;
return $oArtist;
}
?>
<?
$sSeoName = $route[2];
$sSortColumn ="song_name";
$sSortDirection="ASC";
$sWhere = "";
$aArtist = getArtistBySeoName($sSeoName,$sSortColumn,$sSortDirection,$sWhere);
var_dump($aArtist);
Below here the code where I am trying to print the values. This is where it goes wrong probably.
<?
foreach($aArtist as $oSong)
{
//print $aArtist[song_name];
switch($oSong[song_type_name])
{
case "CRD":
$sPagename = CHORDSPAGE;
break;
case "TAB":
$sPagename = TABPAGE;
break;
case "BTAB":
$sPagename = BTABPAGE;
break;
case "LYRIC":
$sPagename = LYRICSPAGE;
break;
case "PTB":
$sPagename = PTBPAGE;
break;
case "GPR":
$sPagename = GPRPAGE;
break;
}
?>
<tr>
<td><span itemprop="name"><? echo ucwords($oSong->song_name);?><span><meta itemprop="url" content ="http://www.gitaartabs.nl/<?=$sPagename?>/<?=$oSong[artist_seoname]?>/<?=$oSong[song_seoname]?>"><meta itemprop="duration" content="PT2M30S"></td>
<td class=""><?=ucfirst($oSong[song_type_name])?></td>
<td class="hidden-sm"><?=ucfirst($oSong[artist_album_name])?></td>
<td class="hidden-sm"><?=ucfirst($oSong[artist_album_year])?></td>
<td class="hidden-sm"><?=ucfirst($oSong[song_style_name])?></td>
<td><em><?=ucfirst($oSong[download_amount])?></em></td>
<td><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i></td>
</tr>
</tbody>
</table>
<!--End Basic Table-->
I fixed this by using a foreach(song as songval) { } code
thanks all for your help

PHP print all the values inside while without repeat html

i got a problem with my php script.
I can't figure out how to print the values of my last query inside the same div with a while cycle, without repeat the div! Here's the code.
$query_serie = "SELECT nome_serie FROM serie";
$result_serie = mysql_query($query_serie);
while ($row_serie = mysql_fetch_array($result_serie)) {
echo '<h4 class="intestazione_catalogo"><span>serie:</span> '.$row_serie['nome_serie'].'</h4>';
$query_finitura = "SELECT nome_finitura FROM finitura JOIN serie ON finitura.id_serie = serie.id_serie WHERE nome_serie = '" .$row_serie['nome_serie']. "'";
$result_finitura = mysql_query($query_finitura);
while ($row_finitura = mysql_fetch_array($result_finitura)) {
echo '<div class="finitura_catalogo">'.$row_finitura['nome_finitura'].'</div>';
$query_codice = "SELECT codice FROM codice JOIN finitura ON codice.id_finitura = finitura.id_finitura LEFT JOIN serie ON finitura.id_serie = serie.id_serie WHERE visibile = 'si' AND nome_finitura = '".$row_finitura['nome_finitura']."'";
$result_codice = mysql_query($query_codice);
while ($row_codice = mysql_fetch_array($result_codice)) {
echo '<div class="codice_catalogo">'.$row_codice['codice'].'</div>';
}
echo '<div class="clear"></div>';
}
}
I need to ouput something like this
and following the others blocks and go on...
Any help would be appreciated!
Try putting divisions outside the while loop. Something like this:
<div class="codice_catalogo">
while ($row_codice = mysql_fetch_array($result_codice)) {
echo $row_codice['codice'];
}
</div>
Hope it works.

Using php's count () command to count the result of an if statement

I am trying to work my head round this, I am using the following code to check the answers to a quiz and output either CORRECT or INCORRECT depending on the result of the comparison, and if the answer field is black (which only comes from the feedback form) a different message is displayed.
I cant quite work out how to apply the php count function in this situation though, what im trying to get it to do it count the amount of CORRECT and INCORRECT answers and add the two together, and then I can work out a % score from that.
<?php
// Make a MySQL Connection
// Construct our join query
$query = "SELECT * FROM itsnb_chronoforms_data_answerquiz a, itsnb_chronoforms_data_createquestions
q WHERE a.quizID='$quizID' AND a.userID='$userID' and q.quizID=a.quizID and
a.questionID = q.questionID ORDER BY a.cf_id ASC" or die("MySQL ERROR: ".mysql_error());
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
if ($row['correctanswer'] == ''){echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>';}
elseif ($row['correctanswer'] == $row['quizselectanswer']){
echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';}
else {echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>';
}}
?>
Iv found this example and have been trying to work out how to work it in, but cant think how to count the results of an if statement with it ?.
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
$result = count($people);
echo $result;
?>
Just increment two counters
$correct_answers = 0;
$incorrect_answers = 0;
while ($row = mysql_fetch_array($result)) {
if ($row['correctanswer'] == '') {...
} elseif ($row['correctanswer'] == $row['quizselectanswer']) {
echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';
$correct_answers++;
} else {
echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>';
$incorrect_answers++;
}
}
Simply increase some counter in each branch of your if/elseif/else construct...
$counters = array( 'blank'=>0, 'correct'=>0, 'incorrect'=>0 );
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
if ( ''==$row['correctanswer'] ) {
echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>';
$counters['blank'] += 1;
}
elseif ( $row['correctanswer']==$row['quizselectanswer'] ) {
echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';
$counters['correct'] += 1;
}
else {
echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>';
$counters['incorrect'] += 1;
}
}
echo '#correct answers: ', $counters['correct'];
How about creating a variable with a count of correct answers? For each question you loop through, increment the value by one.
<?php
$correct_answers = 0;
while($questions) {
if($answer_is_correct) {
// Increment the number of correct answers.
$correct_answers++;
// Display the message you need to and continue to next question.
}
else {
// Don't increment the number of correct answers, display the message
// you need to and continue to the next question.
}
}
echo 'You got ' . $correct_answers . ' question(s) right!';
<?php
// Make a MySQL Connection
// Construct our join query
$query = "SELECT ... ";
$result = mysql_query($query) or die(mysql_error());
$countCorrect = 0;
$countIncorrect = 0;
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
if ($row['correctanswer'] == ''){echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>';}
elseif ($row['correctanswer'] == $row['quizselectanswer']){
$countCorrect++;
echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';}
else {
$countIncorrect++;
echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>';
}
}
echo ((int)($countCorrect/($countIncorrect + $countCorrect) * 100)) . "% answers were correct!" ;
?>

Categories