Jump to content

isset()


RON_ron

Recommended Posts

I want to get the COUNT of the fields (ArrayM11 - ArrayM19), which has the word "YES". The code isn't working. Appreciate any help.

 

$Array1 = array();
for($j=0;$j<count($ArrayM1);++$j) {
$result =  array_count_values($ArrayM1[$j]);
if (isset($result['YES'])) {
  $points1 = $result['YES'];
}
}

Link to comment
Share on other sites

Below was my code. that works. I'm trying to convert this using the arrays.

 

$Array1 = array($winRa1, $winRa2, $winRa3, $winRa4, $winRa5, $winRa6, $winRa7, $winRa8);
$result = array_count_values($Array1);
if (isset($result['YES'])) {
  $points = $result['YES'];
}

 

 

This is what I'm trying to do now. Basically I need to get the COUNT of the fields which contain the word "YES".

$ArrayM1 = array();
$Var = 'name';
$query =  "SELECT * FROM db WHERE username = '".$Var."'";
$result = mysql_query($query);
$score = mysql_fetch_assoc($result);
foreach (range('1','2') as $ltr) {
$ArrayM1[] = array($score['ro' . $ltr]);
}

$Array1 = array();
for($j=0;$j<count($ArrayM1);++$j) {
$result =  array_count_values($ArrayM1[$j]);
if (isset($result['YES'])) {
  $points1 = $result['YES'];
}
}

Link to comment
Share on other sites

Can you actual explain (in English) what it is your wanting to achieve? Your code is terrible. If you can explain (without examples because they are not helping at all) your goals, we might be able to help you right some decent code.

Link to comment
Share on other sites

really can't understand what you're trying to do.. IF YOU are tying to COUNT how MANY  elements in the ARRAY contain the VALUE "YES"

then  why not just do something like

 

foreach($Array1 as $value){
  $value = strtolower($value);
  if($value == "yes"){
    $countingVariable++;
  }
}

Link to comment
Share on other sites

really can't understand what you're trying to do.. IF YOU are tying to COUNT how MANY  elements in the ARRAY contain the VALUE "YES"

then  why not just do something like

 

foreach($Array1 as $value){
  $value = strtolower($value);
  if($value == "yes"){
    $countingVariable++;
  }
}

 

Because....

 

$result = array_count_values($Array1);
if (isset($result['YES'])) {
  $points = $result['YES'];
}

 

is more efficient.

Link to comment
Share on other sites

This is my existing code and I need to get the COUNT of the fields which contain the word "YES" out of my_db2.

 

 

$S1 = array();
$Array1 = array();
$Variable1 = 'somename';
$query1 =  "SELECT * FROM my_db1 WHERE username = '".$Variable1."'";
$result1 = mysql_query($query1);
$score1 = mysql_fetch_assoc($result1);
foreach (range('a','e') as $ltr) {
$S1[] = array($score1['mopA' . $ltr]);
}

$Variable2 = 'nextX';
$query1 =  "SELECT * FROM my_db2 WHERE rdx = '".$Variable2."'";
$result = mysql_query($query1);
$scoreM = mysql_fetch_assoc($result);
foreach (range('a','e') as $ltr) {
$Array1[] = array($scoreM['rowB' . $ltr]);
}
$count = array();
for($i=0;$i<count($S1);++$i) {
$count[$i] = count(array_intersect($S1[$i],$Array1[$i]));
}
$points = array_sum($count);
echo $points;

 

The below code worked before I brought in the arrays. But now I'm dealing with arrays and I'm not knowing how to get the count of the fields which contain the word "YES" in my_db2.

 

$result = array_count_values($Array1);
if (isset($result['YES'])) {
  $points = $result['YES'];
}

 

Thanks and appreciate for helping!

 

Link to comment
Share on other sites

Since you are only returning one row in your query mysql_fetch_assoc isn't necessary--- if you were using that the syntax would require a while loop after the if statement, however this would work if you're just looking for the counted records:


$sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)";
if ($result = mysql_query($sql)) {
     $row = mysql_fetch_row($result);
     echo $row['cnt'];
}

Link to comment
Share on other sites

Hi sweeb.... Thanks (and welcome)

 

Actually I want to get the COUNT of 'YES' from the entire databse.

 

E.g.

rowBa  |  rowBb  |  rowBc  |  rowBd  |  rowBe

NO      |    YES    |    NO    |    NO    |    NO

  NO    |    NO      |  YES    |  YES    |    YES

NO      |    YES    |    NO    |    YES    |    NO

 

count = 6

 

Link to comment
Share on other sites

Since you are only returning one row in your query mysql_fetch_assoc isn't necessary--- if you were using that the syntax would require a while loop after the if statement, however this would work if you're just looking for the counted records:


$sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)";
if ($result = mysql_query($sql)) {
     $row = mysql_fetch_row($result);
     echo $row['cnt'];
}

 

mysql_fetch_assoc() returns an array just like mysql_fetch_row() does. *assoc returns an associative array though while *row returns a numerically indexed array so your syntax would fail because the cnt index does not exist. And in both cases there is no need for any while loop. Maybe you where thinking of....

 


$sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)";
if ($result = mysql_query($sql)) {
  echo mysql_result($result, 0);
}

 

which is shorter, but IMO less clear.

Link to comment
Share on other sites

Hi sweeb.... Thanks (and welcome)

 

Actually I want to get the COUNT of 'YES' from the entire databse.

 

E.g.

rowBa  |  rowBb  |  rowBc  |  rowBd  |  rowBe

NO      |    YES    |    NO    |    NO    |    NO

  NO    |    NO      |  YES    |  YES    |    YES

NO      |    YES    |    NO    |    YES    |    NO

 

count = 6

 

 

To do that your going to have to do it the hard way. however, before going down that path, I would put it to you that your database is poorly designed. Why would you have 5 fields all containing the same or similar data? I should have mentioned this in my previous post. Any time you see field names like..... row1, row2, row3, row4 in a database says its a poor design.

 

Look up some tutorials on database normalization and come back when your data is stored properly. Actually, you probably won't need to because a simple query will get you the answers you need. That's one of the benefits of good DB design.

Link to comment
Share on other sites

Why would you have 5 fields all containing the same or similar data?

I really appreciate your input thorpe.

 

Those columns actually represents Months and rowBa, rowBb, rowBc... row stands for the project we're doing and B stands for the team name. Then 1, 2, 3... to differentiate the fields.  Is it still poorly designed?

 

Thank you so much for the additional input.  :D

Link to comment
Share on other sites

Those columns actually represents Months and rowBa, rowBb, rowBc... row stands for the project we're doing and B stands for the team name. Then 1, 2, 3... to differentiate the fields.  Is it still poorly designed?

 

It would be better off being made into a relational database where you had one table for team declaration with any other info about that team, such as

 

Table Teams

ID - TeamName

1 - Team1

2 - Team2

3 - Team3

 

Table ContainsInfo

Team ID - Data - Month - ProjName

1 - YES - 2 - Alpha

2 - NO - 3 - Beta

3 - YES - 4 - Beta

 

This way you only have to query one column for data results, and you can easily filter by Month or Team if needed and return Team info by using joined queries.

 

 

 

Link to comment
Share on other sites

Sorry for being babyish! But I'm not knowing what to do here.

 

When I insert that piece of code it gives this;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

 

 

$S1 = array();
$Array1 = array();
$Variable1 = 'somename';
$query1 =  "SELECT * FROM my_db1 WHERE username = '".$Variable1."'";
$result1 = mysql_query($query1);
$score1 = mysql_fetch_assoc($result1);
foreach (range('a','e') as $ltr) {
$S1[] = array($score1['mopA' . $ltr]);
}

$Variable2 = 'nextX';
$query1 =  "SELECT * FROM my_db2 WHERE rdx = '".$Variable2."'";
$result = mysql_query($query1);
$scoreM = mysql_fetch_assoc($result);
foreach (range('a','e') as $ltr) {
$Array1[] = array($scoreM['rowB' . $ltr]);
}
$count = array();
for($i=0;$i<count($S1);++$i) {
$count[$i] = count(array_intersect($S1[$i],$Array1[$i]));
}
$points = array_sum($count);
echo $points;

$sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)";
if ($result = mysql_query($sql)) {
$output = mysql_result($result, 0);
}
echo $output;

 

Thanks for helping me out.

Link to comment
Share on other sites

That will not give you a total count of the word 'YES'. It will only give you a count of how many rows contain the word 'YES'.

 

Like I said, You'll need to read some tutorials on database normalization (or even take a look at sweeb's reply), and fix your db design before you can move forward.

Link to comment
Share on other sites

OK. I've used the "COUNT" command before but not with the "IN" command.

 

$sql = "SELECT rowBa, rowBb, rowBc, rowBd, rowBe COUNT(*) AS cnt FROM my_db2 WHERE rowBa, rowBb, rowBc, rowBd, rowBe = 'YES'";
if ($result = mysql_query($sql)) {
$output = mysql_result($result, 0);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.