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

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];
}
}

Related

Undefined offset: 1 after workin offset o

i want to add same values to different id number than i make this code but it say " Undefined offset: 1 "after adding only one value to data base
her is may code
$d = 0;
$sql="SELECT * FROM emplyeelist WHERE StatuseUnder = '$EmpIdSE'";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_assoc($result)){
echo "$row['fullName']"."<input type = 'Checkbox' name = 'EmpName[$d]' value = '".$row['fullName']."'>";
++$d;
}
$size = count($_POST['EmpName']);
$d = 0;
while ($d < $size){
$EmpName = $_POST['EmpName'][$d];
$sql = "INSERT INTO ...";
$result = mysqli_query($db,$sql);
++$d;
}
i finally change my cod to this
<?php
$d = 0;
$sql="SELECT * FROM emplyeelist WHERE StatuseUnder = '$EmpIdSE'";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_assoc($result)){
echo "<input type = 'text' readonly name = 'EmpName[$d]' value ='".$row['fullName']."'>".
"<input type = 'checkbox' readonly name = 'CheckBoxEmpName[$d]' value = '".$row['fullName']."'>"."</br>";
++$d;}
?>
$d = 0;
while ($d < $size){
$EmpName = $_POST['EmpName'][$d];
$CheckBoxEmpName = isset( $_POST['CheckBoxEmpName'][$d]) $_POST['CheckBoxEmpName'][$d] : "";
if(!empty($CheckBoxEmpName)){
$sql = "INSERT INTO table_name (table_row_name)VALUES ('$EmpName')";
$result = mysqli_query($db,$sql);
}++$d;}
than All done all work wall

Display Last userID in php from MySQL

I am trying to echo last userID stored in MySQL. But it is not shown. No error is displayed.
I tried:
$stmti = $reg_user->runQuery("SELECT * FROM tbl_users");
$rowi = $stmti->fetch(PDO::FETCH_ASSOC);
$tf = $rowi['userID'];
echo $tf;
/* NOTE: This works only with mysqli
change the parameters "host","username","password","db_name" with your
own ones
*/
$link = mysqli_connect("host","username","password","db_name");
$query = " SELECT *
FROM tbl_users";
$rows = array();
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
$rows[] = $row;
$lastID = 0;
for ($i = 0 ; $i < count($rows) ; $i++)
$lastID = $i;
echo $rows[$lastID]["userID"];
try this :
$sth = $reg_user->prepare("SELECT MAX(userID) as userID FROM tbl_users");
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
$tf= ($result['userID']);
echo $tf;

Check if FK order is numbered correctly

I have some absences, and each absence has an FK to an employee, which I wanna display in a generated HTML table. However, if I delete an employee the order changes from 0, 1, 2 to 1, 2 (for example if the second employee got deleted).
This messed up my code, because I need the FK to check where I have to insert the leave (in which <tr>). Here I count the employees:
$result = mysql_query("select count(1) FROM employee");
$row = mysql_fetch_array($result);
$count_user = $row[0];
Later in my code I make a query in a loop. The loop runs as many times as there are users. And heres the problem: if an employee gets deleted it will not reach the other one ($i goes 0 and then 1 if there are 2 users, but if one is deleted the FK is 3, so it would need to go one further).
to
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = $i");
while ($row = mysql_fetch_assoc($result)) {
$array_absences[] = $row;
}
Has anyone an Idea that is not based on the FK order?
Also this topic needs a better title so feel free to edit it.
<html>
<head>
<title>Absence System</title>
</head>
<body>
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("absence_system", $con);
$result = mysql_query("select count(1) FROM employee");
$row = mysql_fetch_array($result);
$count_user = $row[0];
$result = mysql_query("select count(1) FROM absences");
$row = mysql_fetch_array($result);
$count_absences = $row[0];
$result = mysql_query("select name, surename, employee_ID FROM employee");
while ($row = mysql_fetch_assoc($result)) {
$array_user[] = $row;
}
for($i = 0; $i < $count_user; $i++){
echo '<table = border = 1px>';
echo '</tr>';
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = $i");
while ($row = mysql_fetch_assoc($result)) {
$array_absences[] = $row;
}
$count = count($array_absences);
echo $count;
print_r($array_absences);
for($j = 0; $j < 32; $j++){
$true = 0;
if($j == 0){
echo '<td>';
echo $array_user[$i]['name'], " ", $array_user[$i]['surename'] ;
echo '</td>';
}
for($k = 0; $k < $count; $k++)
{
$array_absences[$k]['start'] = substr($array_absences[$k]['start'], -2);
$array_absences[$k]['end'] = substr($array_absences[$k]['end'], -2);
$array_absences[$k]['start'] = ereg_replace("^0", "", $array_absences[$k]['start']);
$array_absences[$k]['end'] = ereg_replace("^0", "", $array_absences[$k]['end']);
if($j == $array_absences[$k]['start'] && $array_absences[$k]['employee_FK'] == $i){
$true = 1;
echo '<td>';
echo $array_absences[$k]['type_FK'];
echo '</td>';
}
}
if($j != 0 && $true == 0){
echo '<td>';
echo "$j";
echo '</td>';
}
}
echo '</tr>';
echo '</table>';
}
?>
</body>
</html>
If employee_ID corresponds to employee_FK, then you should not iterate over $count_user, but rather over $array_user - then your select would looke like this:
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = {$array_user[$i]['employee_ID']}");
while $i will still represent sequence number, because
$count_user == count($array_user)

PHP Counting MYSQL rows issue?

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";
?>

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