There are three "BC" in the result_category but I am only getting 1 result. This is for a personality quiz. Please Help. I also tried $query = "SELECT result FROM quiz_map where result_category = 'BC'"; but still, only 1 result is showing.
$result = mysqli_query($link, $query);
$cat_a = $cat_b = $cat_c = $cat_d = $cat_e = 0;
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
$cat = $row['category'];
if ($cat == "A") {
$cat_a += 1;
} elseif ($cat == "B") {
$cat_b += 1;
} elseif ($cat == "C") {
$cat_c += 1;
} elseif ($cat == "D") {
$cat_d += 1;
} elseif ($cat == "E") {
$cat_e += 1;
}
}
$array = array('A' => $cat_a, 'B' => $cat_b, 'C' => $cat_c, 'D' => $cat_d, 'E' => $cat_e);
$str = '';
foreach ($array as $i => $value) {
if ($value >= 6) {
$str = $i;
break;
} elseif ($value >= 2) {
$str .= $i;
}
}
$var = sort($array);
$query = "SELECT result FROM quiz_map where result_category = '$str' LIMIT 1";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo $row[0];
?>
There's many things wrong with your code!
As pointed by #IsThisJavascript and #Cashbee:
You are executing a query with a LIMIT 1 statement, it will only return one record.
As pointed by myself:
Doing echo $row[0] will have the same result, if you are only echoing the first value of the array you can't expect to have multiples can you?
As pointed by #IsThisJavascript:
You need to loop the results array, like so:
while($row = mysqli_fetch_array($result)){
echo $row['result_category'];
}
Consider switching the query from a '=' to a '%like%' statement, to maximize results if you want to get the values that partionaly cointain the string.
Related
I know the title isn't that good, BUT I really don't know how to describe it in one setence and I need help.
Currently I have this type of disastrous code that count every $type of product there is in the DB (I have 20-30 types, not 2 only):
$cong = 0;
$refr = 0;
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$arr[] = $row;
if ($arr[$t]['product_type'] == 'Congélateur') {
$cong += 1;
}
if ($arr[$t]['product_type'] == 'Réfrigérateur') {
$refr += 1;
}
$t++;
}
}
And I do stuff with it after. But I was disgusted to see that code, so I tried to minimize it, but I can't figure it out, I tried that:
<php?
$t = 0;
$type = array(
"cong" => "Congélateur",
"refr" => "Réfrigérateur",
);
extract($type, EXTR_PREFIX_SAME, "");
if (mysqli_num_rows($result) > 0) {
foreach ($type as $type_n => $type_m) {
$$type_n = 1;
while ($row = mysqli_fetch_assoc($result)) {
$arr[] = $row;
foreach ($type as $type_n2 => $type_m2) {
if ($arr[$t]['product_type'] == $type_m2) {
$$type_n++;
}
}
$t++;
}
echo $type_n . "=" . $$type_n . "<br>";
}
}
?>
Output:
cong=9
refr=1
What we should expect regarding the DB (In the database, there's 2 cong and 6 refr):
cong=2
refr=6
Any sugestion? Thank you!
You're really making this more complicated than it needs to be. Variable variables are almost never the solution and can always be replaced with an array.
The code you're working with makes no sense, why is it assigning $row to another array and then using a counter to access it? The answer you accepted includes 4 loops, one of them nested.
if (mysqli_num_rows($result) === 0) {
// always exit early rather than indent yourself to death
return false;
}
while ($row = mysqli_fetch_assoc($result)) {
$type = $row["product_type"];
$counts[$type] = ($counts[$type] ?? 0) + 1;
}
print_r($counts);
Demo: https://3v4l.org/8cuis#focus=7.3.28
Counters (which count the type names in your example) should be initialised to 0 not to 1.
You also need to increment $$type_n2 in DB read loop. I'd avoid variable variables if possible.
<php?
$t = 0;
$type = array(
"cong" => "Congélateur",
"refr" => "Réfrigérateur",
);
extract($type, EXTR_PREFIX_SAME, "");
if (mysqli_num_rows($result) > 0) {
foreach ($type as $type_n => $type_m) {
$$type_n = 0;
}
while ($row = mysqli_fetch_assoc($result)) {
$arr[] = $row;
foreach ($type as $type_n2 => $type_m2) {
if ($arr[$t]['product_type'] == $type_m2) {
$$type_n2++; // not $$type_n
}
}
$t++;
}
foreach ($type as $type_n => $type_m) {
echo $type_n . "=" . $$type_n . "<br>";
}
}
?>
I've got a very minor problem but it's one I'd like to work out, so I'm looking for suggestions.
I'm using the following PHP code to echo the data from an array.
$array = array();
$rsa = $wpdb->get_results("SELECT * from wp_letter ORDER BY l_name ASC");
foreach($rsa as $rrr)
{
array_push($array,$rrr->l_name);
}
$temp = 0;
foreach($array as $as)
{
$temp = 0;
$query = $wpdb->get_results("SELECT * FROM wp_term_taxonomy AS tx, wp_terms AS trm WHERE taxonomy = 'category' AND trm.term_id = tx.term_id AND name like '".$as."_%' ORDER BY name ASC");
foreach($query as $row1)
{
if($temp == 0)
{
echo '<h4>'. $as.'</h4>';
$temp = 1;
}
?>
<a class="anchor" href="<?php echo esc_url(get_category_link($row1->term_id)); ?>"><?php echo $row1->name;?></a><br/>
<?php }
} ?>
Just add the LIMIT 3 into your Query, e.g.
SELECT * from wp_letter LIMIT 3 ORDER BY l_name ASC;
You could either use some crap like (for example):
$query = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
$results = ['1col' => [], '2col' => [], '3col' => []];
foreach($query as $key => $result) {
if (($key+1) % 2 === 0 && !(($key + 1) % 3 === 0)) {
$results['2col'][] = $result;
} elseif (($key+1) % 3 === 0) {
$results['3col'][] = $result;
} else {
$results['1col'][] = $result;
}
}
OR just smth like this:
$results = array_chunk($query, ceil(count($query)/3));
Links:
array_chunk
ceil
hey guys im trying to get the even indexes of a string from the db then save them in a variable then echo. but my codes seems doesnt work. please help. here it is
require_once('DBconnect.php');
$school_id = '1';
$section_id = '39';
$select_pk = "SELECT * FROM section
WHERE school_id = '$school_id'
AND section_id = '$section_id'";
$query = mysql_query($select_pk) or die (mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$public_key = $row['public_key'];
}
if ($public_key) {
$leng_public_key = strlen($public_key);
$priv_key_extract = "";
$array_pki = array();
for ($i=0; $i <=$leng_public_key-1 ; $i++) {
array_push($array_pki,$public_key[$i]);
}
foreach ($array_pki as $key => $value) {
if($key % 2 == 0) {
$priv_key_extract += $public_key[$key];
} else {
$priv_key_extract ="haiiizzz";
}
}
}
echo $priv_key_extract;
as you can see im trying to use modulo 2 to see if the index is even.
I have updated your code as below, it will work now :
<?php
$public_key = 'A0L8V1I5N9';
if ($public_key) {
$leng_public_key = strlen($public_key);
$priv_key_extract = "";
$array_pki = array();
for ($i=0; $i <=$leng_public_key-1 ; $i++) {
array_push($array_pki,$public_key[$i]);
}
foreach ($array_pki as $key => $value) {
//Changed condition below $key % 2 ==0 => replaced with $key % 2 == 1
if($key % 2 == 1) {
// Changed concatenation operator , += replaced with .=
$priv_key_extract .= $public_key[$key];
} /*else {
//Commented this as it is getting overwritten
$priv_key_extract ="haiiizzz";
}*/
}
}
echo $priv_key_extract;
?>
Try this function
function extractKey($key) {
if (empty($key) || !is_string($key)) return '';
$pkey = '';
for ($i=0;$i<strlen($key);$i++) {
if ($i % 2 == 0) {
$pkey .= $key[$i];
}
}
return $pkey;
}
echo extractKey('12345678'); # => 1357
I can't remove an item from query result array in php + codeigniter.
This is my code
if($query->num_rows > 0)
{
$rows = $query->result();
foreach ($rows as $key => $row)
{
$i = 0;
$fornecedor = $row->fornecedor;
$marca = $row->marca;
$modelo = $row->modelo;
$versao = $row->versao;
$preco = $row->preco;
foreach ($rows as $row2)
{
$fornecedor2 = $row2->fornecedor;
$marca2 = $row2->marca;
$modelo2 = $row2->modelo;
$versao2 = $row2->versao;
$preco2 = $row2->preco;
if(($fornecedor == $fornecedor2) && ($marca == $marca2) && ($modelo == $modelo2) && ($versao == $versao2) && ($preco == $preco2))
{
$i++;
}
}
if($i > 3)
{
unset($row[$key]);
}
}
return $query;
}
I already checked some examples here in stackoverflow but i cant make this work.
I can't see the problem ty
so $row is a $rows[$key], maybe i don't understand something, but it's seems to me you have to write unset($rows[$key]);
I have a code where it should check if the result equals to 8 it need to show something and if not it need to show something else and all of that happens inside of a while loop.
while ($row_fpages2 = mysql_fetch_array($result_fanpage2))
{
if ( $row_fpages2['client'] != NULL ) {
//GRAPHS
$sql = "SELECT likes, date FROM statistics_pages WHERE idnum = '".$idnum."' AND page_name = '".$row_fpages2['page_name']."' ORDER BY `statistics_pages`.`date` DESC LIMIT 8";
$result2 = mysql_query($sql) or die(mysql_error());
if ($result2) {
$data = array();
while ($row = mysql_fetch_assoc($result2)) {
$data[] = $row["likes"];
}
if ($result2 == 8) {
$c_data = count($data)-1;
$final = array();
for ($i = 0; $i < $c_data; $i++) {
$final[] = getZeroResult($data[$i], $data[$i+1]);
}
$data_string = join(",", $final);
$stats = '<img src="http://chart.apis.google.com/chart?chs=240x140&cht=ls&chd=t:0,0|'.$data_string.'&chg=20,20&chls=0.75,-1,-1|6,4,1&chm=o,FF9900,1,-1,7,-1|b,3399CC44,0,1,0"></img>';
} else {
$stats = '<img src="images/stats_un.jpg"></img>';
};
} else {
print('MySQL query failed with error: ' . mysql_error());
}
echo '...';
The problem is that the first output always showing the ( == 8) (even if it is not equals to 8) instead of the else output.
Then if i have 2 or more everything comes above the first one is correct but the first one is still showing the ( == 8).
Any help?
You do the following which is incorrect:
$result2 = mysql_query($sql) or die(mysql_error());
...
if ($result2 == 8) {
The return value of mysql_query is a resource not an int. What is that you are trying to do there ?
May be you would like to use
if(strlen($result2) == 8){
...
}
instead of
if($result2 == 8){
...
}
Hope that solves your problem.