This question already has answers here:
Can I echo multiple html tags containing php variables with a single echo?
(10 answers)
Closed 11 months ago.
Is there a way to add some kind of parameters to array (yet unknown variables)? As you can see here, i dont know the userID in advance (before the mysql fetch), so i cant properly form a link leading to edit page.
<?php
$box = array ('1'=>"<a href='edit.php?id=/PROBLEM??/'>edit</a>",'2'=>'Cannot edit');
while ($row = mysql_fetch_array($something)) {
?>
<tr>
<td><?php echo $row["Name"]; ?></td>
<td><?php echo $box[$row["editable"]]; ?></td>
</tr>
<?php
}
?>
$row["editable"] returns 1 or 2, depends on database record which
returns if user is editable or not.
You can use sprintf():
$box = array ('1'=>"<a href='edit.php?id=%d'>edit</a>",'2'=>'Cannot edit');
echo sprintf($box[$row["editable"]], ID_HERE)
Do it this way around...
<?php while ($row = mysql_fetch_array($something)) : ?>
<tr>
<td><?php echo $row["Name"]; ?></td>
<?php if( $row["editable"] === 1 ) : ?>
<td><a href='edit.php?id=<?php echo $row["Id"]; ?>'>edit</a></td>
<?php else : ?>
<td>Cannot edit</td>
<?php endif; ?>
</tr>
<?php endif; ?>
Try str_replace():
$box = array ('1'=>"<a href='edit.php?id=%ID%'>edit</a>",'2'=>'Cannot edit');
$link = str_replace('%ID%', $row["id"], $box[$row["editable"]]);
Related
This question already has answers here:
Checking if mysqli_query returned any values?
(2 answers)
Closed 3 months ago.
I'm using while loop to fetch data from the database I want to display like nothing found the result is null or empty. I've tried using if/else with like: if (empty($jobs)) echo 'nothing found'; that doesn't work. if(!mysqli_fetch_assoc($jobs)) works but if don't show the jobs if available.
Loop
<?php while ($job = mysqli_fetch_assoc($jobs)) { ?>
<tr class="custom-table-body-titles">
<td><?php echo h($job['updated_at']); ?></td>
<td>
<?php echo h($job['title']); ?>
</td>
<td>0/<?php echo h($job['required_freelancers']); ?></td>
<td><?php echo h($job['delivery_time']); ?> Days</td>
<td>$<?php echo h($job['budget']); ?></td>
<td>
Apply
</td>
</tr>
<?php } ?>
You have to do a validation to check if any results were found before even starting the loop. Assuming $jobs is an instance of mysqli_result returned by mysqli_query().
if (mysqli_num_rows($jobs) > 0)
{
// your while loop
}
else
{
echo "no jobs found";
}
You should keep PHP and HTML separate as much as you can.
You can fetch all results into an array before and then foreach on that array.
$jobs = $jobs->fetch_all(MYSQLI_ASSOC);
if($jobs) {
foreach ($jobs as $job) {
}
} else {
}
A good question which is sadly most of time is answered incorrectly.
It's a very bad practice to mix the database-related code and HTML code. There should never be a while loop like this.
Instead, the data should be fetched into array,
$jobs = [];
while ($row = mysqli_fetch_assoc($result)) {
$jobs[] = $row;
}
which is then used to output the data. And once you have this array, you can use it to tell whether the query returned any data or not:
<?php if ($jobs) { ?>
<?php foreach ($jobs as $job) { ?>
<tr class="custom-table-body-titles">
<td><?php echo h($job['updated_at']); ?></td>
<td>
<?php echo h($job['title']); ?>
</td>
<td>0/<?php echo h($job['required_freelancers']); ?></td>
<td><?php echo h($job['delivery_time']); ?> Days</td>
<td>$<?php echo h($job['budget']); ?></td>
<td>
Apply
</td>
</tr>
<?php } ?>
<?php } else {?>
no data
<?php } ?>
I have a number of rows (ingredients) in a table in my database, I'm using the following code to iterate and populate my view with the name of each, but I want to be able to display the children/parents (columns in the DB) of each item, I'm able to log out the values correctly but when I try to echo as below I'm receiving "Message: Undefined index: parents" & "Message: Undefined index: children";
<?php foreach ($ingredient as $row => $key) { ?>
<tr>
<td><?php echo $row + 1; ?> </td>
<td><?php echo $key['name']; ?> </td>
<?php ChromePhp::log($key['parents']); ?>
<td><?php echo $key['parents']; ?> </td>
<?php ChromePhp::log($key['children']); ?>
<td><?php echo $key['children']; ?> </td>
<td>
</tr>
<?php } ?>
I'm confused as I have no issue logging it out.
I assumed it may be due to null values so I assigned child/parent a string value for each row as follows;
but the error persists.
Here is the logged output in the browser;
Output of <?php ChromePhp::log($key); ?>
EDIT: Here is how I'm building the $ingredients array;
<?php
function get_ingredient()
{
$qry = "SELECT CONCAT(UPPER(LEFT(i.name, 1)), LCASE(SUBSTRING(`name`, 2))) as name,i.id as id,'ingredient' as type, i.parents as parents, i.children as children FROM `ingredient` i WHERE i.is_del=0 order by i.name asc";
$qry = $this->db->query($qry);
if ($qry->num_rows() > 0) {
$ingr = $qry->result_array();
} else {
$ingr = array();
}
}
?>
I assume I'm missing something fundamental, any advise would be most appreciated. Cheers.
My array had two sets of data, I have resolved this by limiting the array to one set.
<?php foreach ($ingredient as $row => $key) { ?>
<tr>
<td><?php echo $row + 1; ?> </td>
<?php if(isset($key['name']) && !empty($key['name'])):?>
<td><?php echo $key['name']; ?> </td>
<?php endif;?>
<?php if(isset($key['parents']) && !empty($key['parents'])):?>
<?php ChromePhp::log($key['parents']); ?>
<td><?php echo $key['parents']; ?> </td>
<?php endif;?>
<?php if(isset($key['children']) && !empty($key['children'])):?>
<?php ChromePhp::log($key['children']); ?>
<td><?php echo $key['children']; ?> </td>
<?php endif;?>
</tr>
$key looks like an object so you have to access it with -> so to get value of name use $key->name,$key->parents
fetching some data from mysqli
$value=mysqli_fetch_assoc($query)
I have a result like
$value = "123456789101112131415161718192021222324252627282930";
but I want to use it two variables like
$value1 = "123456789101112131415";
$value2 = "161718192021222324252627282930";
Use the database column names.
If you don't have the value you need in separate database column you have to somehow extract the value from an existing column.
You can use substr or regex to do so.
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['DATABASE_COLUMN_1_NAME']; ?></td>
<td><?php echo $row['DATABASE_COLUMN_2_NAME']; ?></td>
<td><?php echo $row['DATABASE_COLUMN_3_NAME']; ?></td>
<!-- show only a part of the db columns content -->
<?php $valueIwantToShow = subtr($row['DATABASE_COLUMN_4_NAME'], 0, 5); ?>
<td><?php echo valueIwantToShow; ?></td>
<!-- or show multiple values in one html column -->
<td><?php echo $row['DATABASE_COLUMN_6_NAME']; ?>, <?php echo $row['DATABASE_COLUMN_7_NAME']; ?></td>
</tr>
<?php } ?>
I have table that contain data
<td><?php echo $users->aa_0?></td
<td><?php echo $users->aa_1?></td>
<td><?php echo $users->aa_2?></td>
<td><?php echo $users->aa_3?></td>
when i'm applying static value to above code, it is working
But when i'm changing it to dynamic the below code is not working...
kindly help me
<?php foreach ($get_users as $users) { ?>
<tr>
<td><?php echo $users->aa_.$i?></td>
<?php ?>
</tr>
<?php } ?>
Basically
$users->{"aa_$i"}
But as JJ said, you should use arrays, like
$users->aa[$i]
im having trouble getting a result value of a query and the error says.
Notice: Undefined index: prelim in C:\xampp\htdocs\gradingxworking\teacher\student.php on line 85 can someone help me to fix this or some clue to fix this? im just starting to learn php.
<tbody>
<?php $c=1; ?>
<?php foreach($mystudent as $row): ?>
<tr>
<td><?php echo $c; ?></td>
<td class="text-center"><?php echo $row['studid']; ?></td>
<td class="text-center"><?php echo $row['lname'].', '.$row['fname']; ?></td>
<?php $grade = $student->getstudentgrade($row['studid']);?>
//code that cause error line 85--> <td class="text-center"><?php echo $grade['prelim']; ?></td>
</tr>
<?php $c++; ?>
<?php endforeach; ?>
<?php if(!$mystudent): ?>
<tr><td colspan="8" class="text-center text-danger"><strong>*** No Result ***</strong></td></tr>
<?php endif; ?>
</tbody>
the function:
function getstudentgrade($studid){
$q = "select * from studentsubject where studid=$studid";
$r = mysql_query($q);
$data = array();
while($row = mysql_fetch_array($r)){
$data[] = array(
'prelim' => $row['prelim']
);
}
return $data;
}
As #Bara suggested, you need to check the array first before accessing it.
if(isset($grade['prelim']))
I suspect, there is no data for specific studid. Lets see you function.
$data = array();
while($row = mysql_fetch_array($r)){
$data[] = array(
'prelim' => $row['prelim']
);
}
Now, you have created new array $data. But, if there is no record ? your while loop won't be executed and your $data array won't have anything. right ? So, to handle that, you need to check whether there is any data in your array.
Now, second point, which #Mossavari made is also correct. You need to use
$grade[0]['prelim'];
instead of
$grade['prelim'];
after doing
<?php $grade = $student->getstudentgrade($row['studid']);?>
you need to check what $grade holdes. and its better before trying to fetch data from array to make a check like this :
if(isset($grade['prelim']))
Based on your getstudentgrade function you'll have multidimensional array result, you may need to change $grade['prelim'] to $grade[0]['prelim']
Since, every student will have only 1 grade. So, why to use array.
<?php
function getstudentgrade($studid){
$q = "select * from studentsubject where studid=$studid LIMIT 0,1";
$data = "";
$r = mysql_query($q);
while($row = mysql_fetch_array($r)){
$data = $row['prelim'];
}
return $data;
}?>
PHP
<tbody>
<?php $c=1; ?>
<?php foreach($mystudent as $row): ?>
<tr>
<td><?php echo $c; ?></td>
<td class="text-center"><?php echo $row['studid']; ?></td>
<td class="text-center"><?php echo $row['lname'].', '.$row['fname']; ?></td>
<td class="text-center"><?php echo $grade = $student->getstudentgrade($row['studid']);?></td>
</tr>
<?php $c++; ?>
<?php endforeach; ?>
<?php if(!$mystudent): ?>
<tr>
<td colspan="8" class="text-center text-danger">
<strong>*** No Result ***</strong>
</td>
</tr>
<?php endif; ?>
</tbody>