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
Related
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.
I would like to generate a multidimensional array as follows:
$arrayTwo = [
[ 'This','array','needs','to','be','filled'],
['This','cat','needs','to','be','filled'],
[ 'This','cat','needs','to','get','eaten'],
[ 'This','array','needs','to','get','eaten'],
[ 'This','array','needs','to','count','sheep']
];
I have created code which fills in the blanks using the top result from a database search, based on length and then filtered by google-results, then bing-results, then yahoo-results. However, this means if I have an array which contains multiple instances of two consecutive NULL members, then it will fill them with the same word pair every time. This would not be an issue for the first time $arrayOne is entered into $arrayTwo, as it is a valid response, but if I create a loop then I want the next formulation of $arrayOne to contain the selection of word groups which has the next highest values in the google-/bing-/yahoo-results sections.
Database:
|column 1 |length |google-results |bing-results |yahoo-results |
|array |1 |1213253 |3456365 |253645 |
|be filled |2 |5463656 |4255665 |424535 |
|cat |1 |4564747 |9678744 |345636 |
|get eaten |2 |4543636 |5356456 |424253 |
|count sheep|2 |4355665 |5645647 |476578 |
|umbrella |1 |6973435 |5356356 |346476 |
HTML
$arrayOne = [0=> 'This',1=> ,2=> 'needs',3=> 'to',4=> ,5=> ];
$arrayTwo = [];
array_unshift($arrayOne, 'Suggestion:');
for($i=0;$i<count($arrayOne);$i++) {
if($arrayOne[$i] == NULL) {
if($arrayOne[$i-1] != NULL) {
$counter = 1;
for($x=$i+1;$x<count($arrayOne);$x++) {
if($arrayOne[$x] == NULL && $arrayOne[$x-1] == NULL) {
$counter++;
}
elseif($arrayOne[$x] != NULL && $arrayOne[$x-1] == NULL) {
$queryBlank = "SELECT * FROM table_name WHERE length = ".$counter." ORDER BY `google-results` DESC, `bing-results` DESC, `yahoo-results` DESC";
$resultBlank = mysqli_query($conn,$queryBlank);
if($row = mysqli_fetch_assoc($resultBlank)) {
$blank = explode(' ', $row['column 1']);
foreach($blank as $key => $value) {
$arrayOne[$i+$key] = $value;
}
}
}
}
array_push($arrayTwo, $arrayOne);
}
}
}
I don't know my result is exactly with your mind or not
But I tried my best xD
$arrayOne = [0=> 'This',1=> '',2=> 'needs',3=> 'to',4=> '',5=> ''];
$arrayTwo = [];
$arrayThree = [];
for($i=0;$i<count($arrayOne);$i++) {
if($arrayOne[$i] == NULL) {
if(isset($arrayOne[$i+1]) && $arrayOne[$i+1] != NULL){
$counter = 1;
$queryBlank = "SELECT * FROM table_name WHERE length = ".$counter." ORDER BY `google-results` ASC, `bing-results` ASC, `yahoo-results` ASC";
$resultBlank = mysqli_query($conn,$queryBlank);
while($row = mysqli_fetch_array($resultBlank)) {
// echo $row['column 1'];
$blank = explode(' ', $row['column 1']);
$arrayTemp = array();
foreach($blank as $key => $value) {
$arrayTemp[$i+$key] = $value;
}
array_push($arrayTwo, $arrayTemp);
}
}elseif(isset($arrayOne[$i+1]) && $arrayOne[$i+1] == NULL){
// echo $i;
$counter = 2;
$queryBlank = "SELECT * FROM table_name WHERE length = ".$counter." ORDER BY `google-results` ASC, `bing-results` ASC, `yahoo-results` ASC";
$resultBlank = mysqli_query($conn,$queryBlank);
while($row = mysqli_fetch_array($resultBlank)) {
// echo $row['column 1'];
$blank = explode(' ', $row['column 1']);
$arrayTemp = array();
foreach($blank as $key => $value) {
$arrayTemp[$i+$key] = $value;
}
array_push($arrayThree, $arrayTemp);
}
}
}
}
foreach ($arrayTwo as $key => $value) {
$newArray = array_replace($arrayOne,$value);
foreach ($arrayThree as $key2 => $value2) {
print_r(array_replace($newArray,$value2));
echo '<br>';
}
}
And my result
Why count sheep is the best match. Please recheck your database it is 5645647 > be filled with 5463656
I'm not sure but you can set ASC or DESC with your mind.
Below is my code:
$id = $_GET['id'];
$qty = $_GET['qty'];
$product_id=$_GET['product_id'];
This is how I receive in the browser
http://example.com/shopping_cart.php?id=17,18&qty=4,5&product_id=3
$_SESSION['test'][]= array('product_id'=>$product_id,array('id'=>$id,'qty'=>$qty));
//print_r($_SESSION['test']);
foreach($_SESSION['test'] as $item=>$value)
{
echo "Main Array ID=". $item;
echo "<br/>";
foreach($value as $v=>$v1)
{
if(is_array($v1))
{
echo "Sub Array ID=". $v;
echo "<br/>";
echo "size id=". $v1['id'];
echo "<br/>";
echo "Quantity=". $v1['qty'];
echo "<br/>";
}
}
}
Output:
Main Array ID=0
Sub Array ID=0
size id=12,13
Quantity=1,2
Main Array ID=1
Sub Array ID=0
size id=17,18
Quantity=4,5
Since size_id and quantity are in implode form, I mean they have a comma ',' in between the value. I need to explode them and use foreach to display one by one.
I mean something like this:
$size_id1=explode(',',$v1['qty']);
foreach($size_id1 as $size_id2)
{
echo $size_id2;
}
$qty1=explode(',',$v1['qty']);
foreach($qty1 as $qty2)
{
echo $qty2;
}
What I need is, I want to display matching size_id and qty. For example, instead of displaying:
size_id 1
size_id 2
Qty 1
Qty 2
It should display:
Size_id 1 Qty 1
Size_id 2 Qty 2
How can I achieve this?
$ids = $_GET['id']
$qtys = $_GET['qty']
$product_id = $_GET['product_id'];
$tmp_result = array();
$ids = explode(',',$ids );
$i = 0;
foreach($ids as $id)
{
$tmp_result[$i]['id'] = $id;
$i++;
}
$i = 0;
$qtys =explode(',',$qtys );
foreach($qtys as $qty)
{
$tmp_result[$i]['qty'] = $qty;
$i++;
}
$_SESSION['tmp_results'][$product_id] = $tmp_result;
$temp_arr = array();
$id_str = '17,18';
$qty_str = '4,5';
foreach(array_combine(explode(',', $id_str), explode(',', $qty_str)) as $k => $v) {
$temp_arr[]['id'] = $k;
$temp_arr[]['qty'] = $v;
}
diffrence betwen 2 implode fetch from database
<?php
$select_tbl=mysql_query("select * from user_reg where uid='". $_SESSION['user1']."'");
while($fetch=mysql_fetch_object($select_tbl))
{
$r=$fetch->specialised;
$i=explode(",",$r);
$length = count($i);
for ($x=0; $x<$length; $x++)
{
echo $i[$x]; }
}?><tbody>
<?php
mysql_connect("localhost","root","");
mysql_select_db("freelancer");
$query=mysql_query("select * from post_proj where status = 1 and payment = '' ") or die(mysql_error());
$j = 1;
while($result=mysql_fetch_array($query))
{
if($result['uid'] == "")
{$d2 = $result['proj_skill'];
$pantry_food1 = explode(",",$d2);
$count_total1 = count($pantry_food1);
for ($counter=0; $counter<$count_total1; $counter++){
$line1 = each ($pantry_food1);
// echo $pantry_food1[$counter];
} $rrt = array_intersect($i,$pantry_food1);
The mysql query result can have 1 or 2 rows with results. How would I separate them?
$bio = mysql_query("SELECT *, FROM soc_meminfo
WHERE m_id = '".mysql_real_escape_string($en['id'])."'");
#
if (mysql_num_rows($bio) == 0) call404();
$line = mysql_fetch_assoc($bio);
foreach ($line as $key => $value) {
$en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value));
}
Try this:
while ($row = mysql_fetch_assoc($bio)) { ... }
Also your sql should be SELECT * FROM soc_meminfo
try this
while ($row = mysql_fetch_assoc($bio)){
foreach ($row as $value){
echo $value;
}
echo '<br>';
}
I'm not sure I understand it, but try this.
$bio = mysql_query("SELECT *, FROM soc_meminfo
WHERE m_id = '".mysql_real_escape_string($en['id'])."'");
#
if (mysql_num_rows($bio) == 0) call404();
$line = mysql_fetch_assoc($bio);
foreach ($line as $key => $value) {
$en['b'.$key][] = str_replace("\n",'<br/>',stripslashes($value));
}
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.