PHP Counting MYSQL rows issue? - php

I am trying to make it to if I have at least 1 team created for one person, it will display the team. If they don't have a team, it will say no teams. It works if the person has at least 1 team, but nothing shows if the person is not on a team. How do I fix this?
<?php
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
$num = 0;
while($row = mysql_fetch_assoc($sql)){
$num = ++$num;
$amount1 = mysql_num_rows($sql);
$name = $row["name"];
$teamrank = $row["rank"];
$teamlink = $row["link"];
$players = $row["players"];
$teamid = $row['id'];
if($amount1 < 1){
$teams = "No Teams";
echo "$amount";
}else{
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
}print "$amount1";
?>

Look at the third line of this code - put it in your code on the same place instead of your line.
if($amount1 < 1){
$teams = "No Teams";
echo $teams;
}else{
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}

Put the counting outside the while... loop
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
$amount1 = mysql_num_rows($sql); //<---- this should fix it
$num = 0;
while($row = mysql_fetch_assoc($sql)){
$num = ++$num;
$name = $row["name"];
$teamrank = $row["rank"];
$teamlink = $row["link"];
$players = $row["players"];
$teamid = $row['id'];
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
if($amount1 < 1){
$teams = "No Teams";
}
print "$amount1";
?>

Related

How can I make this code short and easy to read?

I have more than 10 lines like this and I want to echo just 5 rows per page this is my code.
$c = mysqli_fetch_field_direct;
$c1 = $c($result,2)->name;
$c2 = $c($result,3)->name;
$c3 = $c($result,4)->name;
//$cn,...;
echo "$row[2]" ; echo "$c1[2]";
echo "$row[3]" ; echo "$c1[3]";
//echo "$row[n]" , ...;
Before this code I have this:
$sql = "SELECT * FROM sheet1 WHERE id = '$_POST[text]'";
$result = mysqli_query($con,$sql);
If ($row = mysqli_fetch_array($result))
{
//the 10 lines of codes above
}
You may want to loop through these elements:
$sql = "SELECT * FROM sheet1 WHERE id = '$_POST[text]'";
$result = mysqli_query($con,$sql);
If ($row = mysqli_fetch_array($result))
{
for($i = 2; $i < 10; $i++){
$c = mysqli_fetch_field_direct($result,$i)->name;
echo $row[$i] ; echo $c[$i];
}
}

give a unique number from 1 to 200 for each row? assign a number for each row

I have this small code to display 200 rows and I want to give each row a number a per the no of votes.
How can I do it?
This is what I thought of but it does not work.
$sql = 'SELECT * FROM table LIMIT 200;
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$row = 1;
echo $row++;
echo $row['fullname'];
echo '<br>';
}
the output is making all row output to 1.
edit :
okay so some of the answers did the job but it does not work on the pagination. it counts from 1 on page 2,3,4,5.... heres the code.
<?php
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
$result_per_page = 20;
$no_of_result = mysqli_num_rows($result);
$no_of_page = ceil($no_of_result/$result_per_page);
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$page_first = ($page-1)*$result_per_page;
$sql = 'SELECT * FROM table LIMIT ' . $page_first . ',' . $result_per_page;
$result = mysqli_query($conn, $sql);
$num = 1;
while($row = mysqli_fetch_assoc($result)) {
echo $num++;
echo $row['fullname'];
echo '<br>';
}
for($page=1;$page<=$no_of_page;$page++){
echo ''.$page.'';
echo ' ';
}
?>
Try to read your code line by line and see what it does.
while($row = mysqli_fetch_assoc($result)) { # > Set variable $row to the next result set or exit when there are none.
$row = 1; # Set variable $row to value 1.
echo $row++; # Increment variable $row to + 1.
echo $row['fullname']; # Row is now an integer, not an array. (You did that 2 statements ago)
echo '<br>'; #
} // end of loop, start again --------------|
A solution would be:
$id = 1;
while($row = mysqli_fetch_assoc($result)) {
echo $id++;
echo $row['fullname'];
echo '<br>';
}
As for the page, you're overwriting the previous value:
for($s=$page;$s<=$no_of_page;$s++){
echo ''.$s.'';
echo ' ';
}

Query Debugging.

I have two tables i am using two quires to fetch data from them separately.
$query = mysql_query("SELECT * FROM offers");
$query1 = mysql_query("SELECT * FROM Offers_Dalia ") or die(mysql_error());
I am printing the result on one html-table.
.... the problem is that i want to know that why the loop is running for 11 times both.
1). I am using two while loops. 2). each loop is running for row->mysql_fetch_object times. please help me to find error why my both loops are running for 11 times. total 22 rows are present in html table output. but my both table have so many rows. 100 of rows in both table.
please check the code.
$query = mysql_query("SELECT * FROM offers");
$query1 = mysql_query("SELECT * FROM Offers_Dalia ") or die(mysql_error());
$x = 1;
$n = 0;
while($row = mysql_fetch_object($query))
{
$oid = $row->id;
$name = stripslashes($row->name);
$reward = stripslashes($row->credits);
$network = stripslashes($row->network);
$hits = $row->hits; //clicks; $epc = $row->epc;
$campid = stripslashes($row->campaign_id);
$country = $row->countries;
$url2=$row->link;
$desc=$row->description;
if($x%2 == 0)
{
$trColor = "f1f0f0";
}else
{
$trColor = "ffffff";
}
if(stristr($country, ","))
{
$country_arrs = explode(",", $country);
$country = implode(", ", $country_arrs);
}
?>
<tr style="background:#<?=$trColor?>" ><td><input type="checkbox" value="<?=$oid?>" name="ids[]" onclick="uncheckCheckAllbox(this)" /></td><td><?=$campid?>- campId</td><td><?=$name?></td><td><?=$reward?><td><?=$network?><td><?=$desc?></td><td>Click Here</td></td><td><?=$country?></td></tr>
$n++;
while($row1 = mysql_fetch_object($query1))
{
$uuid = $row1->offers_uuid;
$title1 = stripslashes($row1->title);
$info1 = stripslashes($row1->info_short);
$device_kind1 = stripslashes($row1->device_kinds);
$country1 = stripslashes($row1->target_groups_values);
// $status = toggleStatus($row->active);
$reward_dollar1 = $row1->reward_dollar; //clicks;
$url1 = $row1->url;
$network1 = 'Dalia';
$url1 = urldecode($url1);
if($x%2 == 0)
{
$trColor = "f1f0f0";
}else
{
$trColor = "ffffff";
}
if(stristr($country, ","))
{
$country_arrs = explode(",", $country);
$country = implode(", ", $country_arrs);
}
?>
<tr style="background:#<?=$trColor?>" ><td><input type="checkbox" value="" onclick="uncheckCheckAllbox(this)" /></td><td><?=$uuid?></td><td><?=$title1?></td><td><?=$reward_dollar1?></td><td><?=$network1?></td><td><?=$info1?></td><td>Click Here</td><td><?=$country1?></td></tr>
$n++;
$x++;
goto n;
}
n:
$x++;
}

Why Getting only 1 array instead of many arrays?

I am a completely newbie in programming php I would like to make this code below return many arrays(to flash as3), however I only receive one array.Can anyone please pinpoint what is my mistake here? thanks.
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
echo "returnStr=$data_array";
exit();
}
When you write your exit insight your loop you stop executing your program and you get only one record. You should set the echo and exit after your while loop.
$data_array = "";
$i = 0;
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql)) {
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1) {
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
} else {
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
Those two last line of your should be outside of your loop:
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
If you would name the columns that you want in the SELECT then it's much simpler. Make sure to use MYSQLI_ASSOC in the fetch:
$sql = mysqli_query($conn, "SELECT Username, Fb_id, Access_token, Fb_sig, Char_id FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC))
{
$data_array[] = implode('|', $row);
}
echo "returnStr=" . implode('(||)', $data_array);
exit();

how do I traverse rows in a table

I have a table named members with 3 rows. The following code attempts to display all the rows in the members table. It displays the 1'st record 3 times instead of displaying each record once.
<?php
$con = new mysqli("localhost", "root", "jce123", "profile");
if (mysqli_connect_errno()) {
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
$keysarray = array_keys($array);
$valuearray = array_values($array);
for ($i=0; $i < $num; $i++) {
for($j = 0; $j < count($array); $j++) {
echo "<table>";
echo "<tr><td>".$keysarray[$j].": </td><td>".$valuearray[$j]."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
mysqli_close($con);
?>
I've updated this per suggestions:
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
while ($row = mysqli_fetch_assoc($res)) {
foreach($array as $key => $value){
echo "<table>";
echo "<tr><td>".$key.": </td><td>".$value."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
That's because, your $num = 3, count($array) = 3 and the outer for loop has no bearing on inner for loop.
Where you have $array = mysqli_fetch_assoc($res);, you will only receive one row from your database. You need something like:
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
}
Any basic tutorial will tell you this. Even the PHP docs provide an example.
you can use do something like this..
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
e.g
echo $row['id'];
echo $row['name'];
etc
}
try this... this peace of code is not tested but just to give you an idea...
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
echo "<table>";
echo '<tr><td> id = "'.$row['id'].'":
</td><td> name = "'.$row['name'].'"</td></tr>";
echo "</table>";
}

Categories