I have these values in the array=Array ( [0] => 3 [1] => 2 [2] => 10 [3] => 5 )
How do i get each value separately from the array and perform the below calculation?
$j=$q*100/$ytot;
And this is what i've tried so far, which isnt giving the correct value :
$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
while($row=mysql_fetch_assoc($resul))
{
$y_uid=$row['yes_userid'];
$y_uamt=$row['yes_useramount'];
$n_uid=$row['no_userid'];
$n_uamt=$row['no_useramount'];
$ytot=$row['yes_total'];
$ntot=$row['no_total'];
$hhh=explode(',',$row['yes_useramount']);
$q = print_r($hhh);
echo $j=$q*100/$ytot;
}
}
Try this-
$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
while($row=mysql_fetch_assoc($resul))
{
$y_uid=$row['yes_userid'];
$y_uamt=$row['yes_useramount'];
$n_uid=$row['no_userid'];
$n_uamt=$row['no_useramount'];
$ytot=$row['yes_total'];
$ntot=$row['no_total'];
$hhh=explode(',',$row['yes_useramount']);
}
foreach($hhh as $val)
{
echo $j=$val*100/$ytot;
}
}
A simple foreach should do the trick:
foreach (explode(',', $row['yes_useramount']) as $q) {
echo $q * 100 / $ytot;
}
You can process an array in PHP using foreach like shown below:
foreach (($hhh) as $v => $k)
{
echo $k*100/$ytot;
}
Here,
$hhh=explode(',',$row['yes_useramount']);
So $hhh will return an array of values from $row['yes_useramount'] which was imploded with ,.
try this code in your script. If there having error please tell me here.
$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
while($row=mysql_fetch_assoc($resul))
{
$y_uid=$row['yes_userid'];
$y_uamt[]=$row['yes_useramount'];
$n_uid=$row['no_userid'];
$n_uamt=$row['no_useramount'];
$ytot=$row['yes_total'];
$ntot=$row['no_total'];
}
for($i = 0; $i<count($y_uamt);$i++){
$sum_uamt = $sum_uamt + int($y_uamt[$i]);
}
echo $j = $sum_uamt * 100 / $ytot;
Related
I have two lists of check boxes. I am checking which check box is selected and based on that I am trying to get an array.
Let's say the two lists are size which have small, medium, large boxes checked and the other one is color which have red, green, blue boxes checked. The array should look something like:
array[['small', 'medium', 'large']['red', 'green', 'blue']]
But I am getting this:
array[["small"],["medium"],["large"]] [["red"],["green"],["blue"]]
This is the code:
$counter = 0;
$attributes_list = [];
foreach($features_cuts_list as $k => $features_cut) {
$inner_counter = 0;
if ($features_cut["selectedid"] != "") {
$attributes_list[$counter] = [];
$title_to_get = $features_cut["features_cuts_id"];
/* Gets the name of the box that is checked */
$query = "SELECT title FROM features_cuts_translations WHERE lang = '$lang' AND features_cuts_id = '$title_to_get' LIMIT 1;";
$result = mysql_query($query) or die("Cannot query");
$attribute_name = mysql_fetch_row($result);
foreach ($attribute_name as $q) {
array_push($attributes_list[$counter], $q);
}
$counter++;
} else {
}
}
EDIT:
This is the deceleration process for $features_cuts_list:
function getListValuesSql($sql){
global $link; //Database connection
$data=array();
$subData{0}=array();
$res=mysql_query($sql,$link);
if(mysql_num_rows($res)>0){
$i=0;
while($row=mysql_fetch_array($res)){
for($j=0;$j<mysql_num_fields($res);$j++){
$field=mysql_field_name($res, $j);
$subData{$i}[$field]=$row[$field];
}
$data[$i]=$subData{$i};
$i++;
}
return $data;
}else{
return 0;
}
}
$feature_id = $feature["features_id"];
$features_cuts_list = $data->getListValuesSql("
SELECT DISTINCT fct.*, fc.sort, fc.inner_id, fc.price,
fcp.features_cuts_id AS selectedid, IFNULL(fcpv.price,fc.price)
AS price, fcpv.sku
FROM `features_cuts` as fc
JOIN `features_cuts_translations` as fct ON fct.features_cuts_id=fc.id
LEFT JOIN `features_cuts_product_values` as fcpv ON fc.id=fcpv.features_cuts_id AND fcpv.product_id='$pageid'
LEFT JOIN `features_cuts_products` as fcp ON fc.id=fcp.features_cuts_id AND fcp.product_id='$pageid'
WHERE fc.features_id='$feature_id' AND fct.lang='$lang'
Order by fc.sort
");
Hopes this is helpful
From reading your code I think you have unnecessarily provided the $counter variable.Try this modified code:
$attributes_list = [];
foreach($features_cuts_list as $k => $features_cut) {
$inner_counter = 0;
if ($features_cut["selectedid"] != "") {
$title_to_get = $features_cut["features_cuts_id"];
/* Gets the name of the box that is checked */
$query = "SELECT title FROM features_cuts_translations WHERE lang = '$lang' AND features_cuts_id = '$title_to_get' LIMIT 1;";
$result = mysql_query($query) or die("Cannot query");
$attribute_name = mysql_fetch_row($result);
foreach ($attribute_name as $q) {
array_push($attributes_list, $q);
}
$counter++;
} else {
}
}
If not completely, it will mostly solve your issue.
Let me know what the result are , after running this piece of code.
I am trying to display a single row data using mysqli.
The query is
$getfield = mysqli_query($con,"select name from as_users where user_id=34");
if (mysqli_num_rows($getfield) > 0)
{
while($rowpwd = mysqli_fetch_array($getfield))
{
echo $rowpwd['name'];
}
}
if I print then i get
echo '<pre>';
print_r(mysqli_fetch_array($getfield));
echo '</pre>';
Array
(
[0] => abc
[name] => abc
)
But getting the name inside the while loop doesn't work.
Any help is highly appreciated.
Just try this:
$getfield = mysqli_query($con,"select name from as_users where user_id=34");
if (mysqli_num_rows($getfield) > 0)
{
$rowpwd = mysqli_fetch_array($getfield)['name'];
echo $rowpwd;
}
OR
$getfield = mysqli_query($con,"select name from as_users where user_id=34");
if (mysqli_num_rows($getfield) > 0)
{
$rowpwd = mysqli_fetch_array($getfield);
echo $rowpwd['name'];
}
Try this:
$getfield = mysqli_query($con,"select name from as_users where user_id=34");
if (mysqli_num_rows($getfield) > 0)
$whatYouWant = array();
{
while($rowpwd = mysqli_fetch_array($getfield))
{
//echo $rowpwd['name'];
$whatYouWant[] = $rowpwd['name'];
}
}
echo '<pre>';
print_r($whatYouWant);
echo '</pre>';
Change:
mysqli_fetch_array
To: If you want key of row
mysqli_fetch_assoc
To:If you want index of row
mysqli_fetch_row
I have two tables sport_tbl, match_tbl. In sport_tbl, i defined sport_name such as cricket. In match_tbl, I have match_name,match_date,sport_id.
I want to show match_date of every sport_name (ex. i am showing match_date list for cricket sport and i want to show every date has match_name list).
I want to show one distinct match_date.
Image
my controller code:-
$url = 'cricket' // for example first sport_name
$data['getSportMatch'] = $this->user_model->getSportMatch($url);
my model code:-
public function getSportMatch($sport)
{
$query = $this->db->get_where('match_tbl',array('sport_name' => $sport));
if($query->num_rows > 0)
{
foreach($query->result() as $item){
$data[] = $item;
}
return $data;
}
}
my code in view:-
<div><?php foreach($getSport as $item): ?><h4><?= $item->sport_name; ?></h4><div><?= foreach($getSportMatch as $item): ?>
match_date)) ?>here i want to show list match_name of every match_date
My table structure images
1) sport_tbl
2) match_tbl
3) another match_tbl
you can solve this in model easily. if i did not understand wrong . you need 2 function in model.
1. will get sport names
2. will get matches of given sport name
//model functions
function get_sports(){
$data = array();
$sports = $this->db->select('sport_name')->from('sport_tbl')->get()->result();
if($sports)
{
foreach($sports as $sport){
$data[$sport->sport_name] = $this->get_matches($sport->sport_name);
}
return $data;
}
}
function get_matches($sport_name){
$matches = $this->db->select('*')->from('match_tbl')->where('sport_name',$sport_name)->get()->result();
return $matches;
}
so in view data will be something like this
$data => array(
'cricket'=> array(0 => array(
'match_id' => 11,
'sport_id' = 2 .....
)))
Try this coding ...
public function getSportMatch($sport)
{
$query = $this->db->query("SELECT * FROM sport_tbl as st INNER JOIN match_tbl as mt ON st.sport_id = mt.sport_id WHERE st.sport_name ='".$sport."'");
if($query->num_rows > 0)
{
$query_result = $query->result_array();
$final_result = array();
foreach($query_result as $result ) {
$date = $result['match_date'];
$final_result[$date][] = $result;
}
return $final_result;
}
}
View Coding :-
if(isset($final_result)) {
foreach($final_result as $result) {
echo $result['match_date']; // First display match date
if(isset($result['match_date'])) {
foreach($result['match_date'] as $match) {
echo $match['match_name']; // Second listout the match name
}
}
}
}
I have this code - it's not done by me :s and don't have time to replace the deprecated calls.
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
$db=mysql_connect('localhost','carros_mil1','K-Jw)ureB.}M');
mysql_select_db('carros_mil0',$db);
$nombreListModelo = 'modelo_'.str_replace(" ","_",strtolower($_REQUEST['nombreMarca']));
$sql = "SELECT DISTINCT (identificador) AS id
FROM modelos
WHERE nombre = '".$nombreListModelo."'";
$rs=mysql_query($sql);
$i=0;
while ($row = mysql_fetch_array($rs)) {
$vectorModelo[$i] = $row['id'];
$i++;
}
$sql = "SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = '".$nombreListModelo."'";
$rs = mysql_query($sql);
$row = mysql_fetch_array($rs);
$id1 = $row['attribute_id'];
$sql= "SELECT eao.option_id AS option_id
FROM eav_attribute_option eao, eav_attribute_option_value eaov
WHERE eao.option_id=eaov.option_id AND attribute_id = ".$id1." ORDER BY eaov.value";
$rs=mysql_query($sql);
$retorno = array();
$bandera = true;
while ($row = mysql_fetch_array($rs)) {
$bandera = false;
$sql2= "SELECT value, option_id
FROM eav_attribute_option_value
WHERE option_id = ".$row['option_id']."
ORDER BY value";
$rs2=mysql_query($sql2);
$row2= mysql_fetch_array($rs2);
if ($_REQUEST['bandera']=='vende') {
if (!is_null($row2['value'])) $retorno[$row2['value']] = $row2['value'];
} else {
$existe=false;
for($k=0;$k<count($vectorModelo);$k++) {
if($row2['option_id'] == $vectorModelo[$k]) {
$existe=true;
break;
}
}
if($existe) {
if($_REQUEST['tipo']=='contactenos')
if (!is_null($row2['value'])) $retorno[$row2['value']] = $row2['value'];
else
if (!is_null($row2['option_id'])) $retorno[$row2['option_id']] = $row2['value'];
}
}
}
if($bandera)
$retorno[''] = 'Todos';
header('Content-Type: application/json');
//echo json_encode($retorno);
foreach($retorno as $k => $v) {
printf("%s => %s\n", $k, $v);
}
print_r($retorno);
echo json_encode($retorno);
Since I noticed something strange in the generated json, I added 2 different ways of printing the contents of the array.
The JSON way adds me a null:null entry. The strange case is that I'm checking !is_null($row2['value']) each time I add an element.
When I hit the url under certain parameters, I get:
Elantra => Elantra
Getz => Getz
H1 => H1
i-10 => i-10
New Accent => New Accent
Santa Fé => Santa Fé
Tucson => Tucson
Array
(
[Elantra] => Elantra
[Getz] => Getz
[H1] => H1
[i-10] => i-10
[New Accent] => New Accent
[Santa Fé] => Santa Fé
[Tucson] => Tucson
)
{"Elantra":"Elantra","Getz":"Getz","H1":"H1","i-10":"i-10","New Accent":"New Accent",null:null,"Tucson":"Tucson"}
The first 6 lines correspond to a custom print foreach loop. There's also a print_r call which only shows 6 elements. The 3rd form - which is the actual one I need - shows a null value.
So: Why is $retorno accepting null entries when I check the null condition beforehand? considering this script is full - no missing code here.
(Thanks #Hammerstein - don't know why you didn't write that as an answer).
The problem is that one of such values was not correctly being encoded, since it was not an ANSI value and was not encoded in UTF8. In this way, since "Santa Fé" had a "strange" character, it was encoded as null instead of "Santa F\u00e9".
Tip: Never assume your database is using an utf8_ charset
The solution was to explicitly encode the values when composing the array:
...
$bandera = false;
$sql2= "SELECT value, option_id
FROM eav_attribute_option_value
WHERE option_id = ".$row['option_id']."
ORDER BY value";
$rs2=mysql_query($sql2);
$row2= mysql_fetch_array($rs2);
//ENCODE the values to a good charset
$value = utf8_encode($row2['value']);
$option_id = utf8_encode($row['option_id'])
if ($_REQUEST['bandera']=='vende') {
$retorno[$value] = $value;
} else {
$existe=false;
for($k=0;$k<count($vectorModelo);$k++) {
if($row2['option_id'] == $vectorModelo[$k]) {
$existe=true;
break;
}
}
if($existe) {
if($_REQUEST['tipo']=='contactenos')
$retorno[$value] = $value;
else
$retorno[$option_id] = $value;
}
}
...
I try to write this code on few ways, but always the result is good only for first team all other results are bad.
When I put id of some other club instead of $id I get the good result for that team but than is only one row, I want to show all 20 teams.
<table>
<thead><tr><th>Name</th><th>Played</th><th>0,5</th><th>1,5</th><th>2,5</th>
<th>3,5</th><th>4,5</th></tr></thead>
<tbody>
<?php
$teams = mysql_query("select * from teams");
$num_teams = mysql_num_rows($teams);
while ($group = mysql_fetch_row($teams)) {
$id_team[] = $group;
}
for ($a = 0; $a < $num_teams; $a++) {
if (isset($num_array)) {
mysql_data_seek($query_array, 0 );
$search_array_over = array();
}
$id = $id_team[$a][0];
$name_over = $id_team[$a][1];
$query_array = mysql_query("select * from full_stat where kl1 = $id or kl2 = $id");
$num_array = mysql_num_rows($query_array);
while ($row = mysql_fetch_row($query_array)) {
$data_over[] = $row;
}
$search_array_over = array('1' => '0', '2' => '0' ,'3' => '0', '4' => '0','5' => '0','6' => '0');
for ($now = 0;$now < $num_array; $now++) {
$over_pass = ($data_over[$now][3] + $data_over[$now][4]);
for ($pass = 1; $pass < 7; $pass++) {
if ($over_pass >= $pass) {
$final_pass = $pass;
}
else {
$final_pass = '6';
}
if (array_key_exists($final_pass, $search_array_over)) {
$search_array_over[$final_pass] += 1;
}
else {
$search_array_over[$final_pass] = 1;
}
}
}
echo '<tr><td>'.$name_over.'</td><td>'.$num_array.'</td> <td>'.$search_array_over[1].'</td><td>'.$search_array_over[2].'</td><td>'.$search_array_over[3].'</td><td>'.$search_array_over[4].'</td><td>'.$search_array_over[5].'</td></tr>';
}
?>
</tbody>
</table>
That is one confusing block of PHP code.
A better explanation of your final output would probably help here, but I'm going to guess it's the teamlist plus their statistics.
Since dissecting your current code strikes me as painful, I'm going to describe how I would do this instead.
In your database you have 2 tables, "team" and "team_stats"
Team Structure:
id int
name varchar
....
Stats Structure:
team_id int
someStat int
otherStat int
....
$stmt = $mysqli -> prepare("SELECT * FROM team LEFT JOIN team_stats ON team_stats.team_id");
$stmt -> execute();
$result = $stmt -> fetch_all();
foreach($result as $team) {
// output data to page
}
?>