I've successfully retrieved car make and car name accordingly but I've no idea on how to retrieve image for that particular car.I tried using nested foreach but didn't work as I expected.Instead of displaying particular image for a car id ,it shows the same image from last folder for all ids.
here's my code :
while($row = mysql_fetch_array($result_name,MYSQL_ASSOC)) {
$car[$row['carMake_id']][] = $row['carName'];
//$car_name_id=$row['carName_id'];
$gallery=$row['gallery'];
//$car_make_id=$row['carMake_id'];
//$car_gallery[$row['carName_id']][]=$row['gallery'];
}
foreach ($car as $carmake => $carname) {
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title) {
echo "<td>".$title . "<br/> ";
?>
//this part displays image.. I want it to display according to the car name ($title)
<img src="management/uploads/<?php echo $carmake;?>/<?php echo $gallery;?>" width="100" height="100"></td>
<?php
}
echo'</tr>';
}
Also how do I retrieve all other information pertaining to a particular car inside the foreach?
Thanks.
EDITED PART:
$gallery[]=$row['gallery'];//inside while loop
foreach ($car as $carmake => $carname)
{
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title) {
echo "<td>".$title . "<br/> ";
foreach($gallery as $g)//new foreach to retreive images
{
echo $g;
}
?>
<img src="management/uploads/<?php echo $carmake;?>/<?php echo $g;?>" width="100" height="100"></td>
You need to put gallery into the array.
<?php
while($row=mysql_fetch_array($result_name,MYSQL_ASSOC))
{
$car[$row['carMake_id']][] = $row;
}
foreach ($car as $carmake => $carname)
{
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title)
{
echo "<td>{$title['carName']}<br/> ";
//this part displays image.. I want it to display according to the car name ($title)
echo "<img src='management/uploads/$carmake/{$title['gallery']}' width='100' height='100'></td>";
}
echo'</tr>';
}
?>
Related
I need to convert JSON into a list using PHP, Tried code below but cannot make it work
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$data = json_decode($json);
if (count($data->stand)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->stand as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
And I want to show list as here (not as a table):
http://prntscr.com/no1479
your all code is right but you can use stand class that is wrong your class is GetTickerJSONResult and so change the class stand to GetTickerJSONResult.
try this modified code..
<?PHP
$set =json_decode($json);
if (count($set->GetTickerJSONResult)) {
echo "<table>";
foreach ($set->GetTickerJSONResult as $idx => $stand) {
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
echo "</table>";
}
?>
It does not work because in $data->stand there is nothing.
I have table with some data, and some other data I get with ajax.
I need to know how to display the response inside the table?
Code:
<?php $x = 1; foreach ($projects as $project){
echo "<tr class=\"parent\">";
echo "<td><i class=\"fa fa-chevron-down\"></td>";
echo "<td class=\"pid\">$project[pid]</td>";
echo "<td>$project[status]</td>";
echo "<td>$project[project_title]</td>";
echo "<td>$project[notes]</td>";
echo "<td>$project[responsible]</td>";
echo "<td>$project[start_date]</td>";
echo "<td>$project[completed_date]</td>";
echo "<td>$project[duration]</td>";
echo "<td>$project[completed]</td></tr>";
echo "<div class=\"cchild\" id=\"txtHint$x\">";
echo "</div>";
echo "</tbody>";
$x++;
}
The data should show inside the div element. As far as I know it's not allowed to put the div inside the table, but any other idea. How to get the data properly inside the table.
You first need to initialize a var with starting div and table tag and then in the for loop you can compute and append the tr td tags to the var. Once the loop is completed you can append the closing tags of div and table and the final output of the var will be table. Reference code:
$project = [1,2,3,4,5];
$div = "<div><table><thead></thead><tbody>";
foreach ($project as $p){
$div.="<tr><td>".$p."</td></tr>";
}
$div.="</tbody></table></div>";
You can add an empty tr with in table with specific class like
<?php $x = 1; foreach ($projects as $project){
echo "<tr class=\"parent\">";
echo "<td><i class=\"fa fa-chevron-down\"></td>";
echo "<td class=\"pid\">$project[pid]</td>";
echo "<td>$project[status]</td>";
echo "<td>$project[project_title]</td>";
echo "<td>$project[notes]</td>";
echo "<td>$project[responsible]</td>";
echo "<td>$project[start_date]</td>";
echo "<td>$project[completed_date]</td>";
echo "<td>$project[duration]</td>";
echo "<td>$project[completed]</td></tr>";
echo "<tr><td><div class="cchild"></div></td></tr>";
echo "</tbody>";
$x++;
}
Then replace the cchild div content with your ajax response.
<?php
$bookrec=array(
'book1'=>array('callno'=>123005,'price'=>number_format(1380,2),'desc'=>'Attack on Titan Anthology'),
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge'),
'book3'=>array('callno'=>123004,'price'=>number_format(598,2),'desc'=>'A Work in Progress'),
'book4'=>array('callno'=>123003,'price'=>number_format(668,2),'desc'=>'The Amazing Book is Not on Fire: The World of Dan and Phil'),
'book5'=>array('callno'=>123002,'price'=>number_format(760,2),'desc'=>'Children of Eden: A Novel')
);
$sumbook=array();
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
while(list($booknum,$rec)=each($bookrec)){
echo "<tr>";
foreach($rec as $data){
echo "<td>". $data."<br></td>";
foreach($data as $k => $val){
if(array_key_exists($val,$sumbook))
$sumbook[$val]['price']=$sumbook[$val]['price']+$data['price'];
else if($k == 'desc' && $k == 'callno')
$sumbook[$val] = $data;
}
}
}
echo "<br></tr>";
echo "</table>";
echo "TOTAL AMOUNT: Php ".$sumbook;?>
I am finishing this code for next week. Also, I am a student and just beginning in PHP. My problem is that I am trying to output the sum of ['price'] in the given array but it gives me this error:
Invalid argument supplied for foreach
I also tried various ways on solving this problem but I don't really get it. Please help. :) Thanks!!
<?php
$bookrec=array(
'book1'=>array('callno'=>123005,'price'=>number_format(1380,2),'desc'=>'Attack on Titan Anthology'),
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge'),
'book3'=>array('callno'=>123004,'price'=>number_format(598,2),'desc'=>'A Work in Progress'),
'book4'=>array('callno'=>123003,'price'=>number_format(668,2),'desc'=>'The Amazing Book is Not on Fire: The World of Dan and Phil'),
'book5'=>array('callno'=>123002,'price'=>number_format(760,2),'desc'=>'Children of Eden: A Novel')
);
$sumbook=array();
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
foreach($bookrec as $key1=>$rec )
{
echo "<tr>";
foreach($rec as $key2=>$data)
{
echo "<td>". $data."<br></td>";
if($key2="price")
{
$total+=$data;
}
}
echo "</tr><br>";
}
echo "<br></tr>";
echo "</table>";
echo "TOTAL AMOUNT: Php ".$sumbook;?>
I think you have lots of looping error just follow this code and also understand the how to iterate the array using foreach
$sumbook=array();
$total =0;
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
foreach($bookrec as $key1=>$rec )
{
echo "<tr>";
foreach($rec as $key2=>$data)
{
echo "<td>". $data."<br></td>";
if($key2="price")
{
$total+=$data;
}
}
echo "</tr><br>";
}
echo "</table>";
echo "TOTAL AMOUNT: Php ".$total;
?>
Your first foreach loop had a wrong variable name with the one you initialized as your array.
foreach($bookrec as $data){
}
I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>
I have an array coming from a .csv file. These are coming from a real estate program. In the second column I have the words For Sale and in the third column the words For Rent that are indicated only on the rows that are concerned. Otherwise the cell is empty. I want to display a list rows only For Sale for example. Of course then if the user clicks on a link in one of the rows, the appropriate page will be displayed.
I can't seem to target the text in the column, and I can't permit that the words For Sale be used throughout the entire array because they could appear in another column (description for example).
I have tried this, but to no avail.
/* The array is $arrCSV */
foreach($arrCSV as $book) {
if($book[1] === For Sale) {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
I also tried this:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
Do you mean:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div></div>';
}else{
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
}
I found a solution here:
PHP: Taking Array (CSV) And Intelligently Returning Information
$searchCity = 'For Sale'; //or whatever you are looking for
$file = file_get_contents('annonces.csv');
$results = array();
$lines = explode("\n",$file);
//use any line delim as the 1st param,
//im deciding on \n but idk how your file is encoded
foreach($lines as $line){
//split the line
$col = explode(";",$line);
//and you know city is the 3rd element
if(trim($col[1]) == $searchCity){
$results[] = $col;
}
}
And then:
foreach($results as $Rockband)
{
echo "<tr>";
foreach($Rockband as $item)
{
echo "<td>$item</td>";
}
echo "</tr>";
}
As you can see I'm learning. It turns out that by formulating a question, a series of similar posts are displayed, which is much quicker and easier than using google. Thanks.