简体   繁体   中英

Struggling with php and html concatenating

Hi I have a loop which is outputting from database using php and html. I am trying to concatenate them together and have been messing around from a while now and can't get it correct can someone please give me some help, here is the string

I believe the problem is towards the end.

echo $rowCars['Make']. ' '.$rowCars['Model']. ' '.$rowCars['Age']. ' '.$rowCars['Reg']. ' '.$rowCars['Owner']. ' <img src="assets/images'.$rowCars['Image'].'" />  ''.
            <a href="editdata.php?theid='$rowCars['ID']'">Edit Car</a><br />  }.''; 

I think it should be:

echo $rowCars['Make']. ' '.$rowCars['Model']. ' '.$rowCars['Age']. ' '.$rowCars['Reg']. ' '.$rowCars['Owner']. ' <img src="assets/images'.$rowCars['Image'].'" />'.'<a href="editdata.php?theid='.$rowCars['ID'].'">Edit Car</a><br />';

To help with this in future, you can bring your string into an IDE or even something like Notepad++, set the language settings to PHP, and you'll quickly see what is being interpreted as a string, and what is a variable.

You could also use an online PHP linter for this if you're scratching your head.

You have some mistakes with Quotes and escaping

   echo $rowCars['Make']. ' '.$rowCars['Model']. ' '.$rowCars['Age']. ' '.$rowCars['Reg']. ' '.$rowCars['Owner']. ' <img src="assets/images'.$rowCars['Image'].'" />  ''.
                <a href=\"editdata.php?theid='.$rowCars['ID'].'\">Edit Car</a><br />  }'; 

This should work, but do not concate so much when your struggeling.

$str = $rowCars['Make']. ' '.$rowCars['Model']. ' '.$rowCars['Age']. ' '.$rowCars['Reg']. ' '.$rowCars['Owner'];
$str .= "<img src='assets/images/'".$rowCars['Image'].'/>';
$str .=  "<a href='editdata.php?theid='".$rowCars['ID']."'>Edit Car</a><br />  }";

echo $str;

or

echo $rowCars['Make']. ' '.$rowCars['Model']. ' '.$rowCars['Age']. ' '.$rowCars['Reg']. ' '.$rowCars['Owner'] <img src='assets/images/'".$rowCars['Image'].'/>' <a href='editdata.php?theid='".$rowCars['ID']."'>Edit Car</a><br />  }";

I think this is what you want:

echo 
$rowCars['Make']. 
' '.
$rowCars['Model']. 
' '.
$rowCars['Age']. 
' '.
$rowCars['Reg']. 
' '.
$rowCars['Owner']. 
' <img src="assets/images'.
$rowCars['Image'].
'" />  ''.
'<a href="editdata.php?theid=' . $rowCars['ID'] . '">Edit Car</a><br />  }'.
''; 
echo $rowCars['Make']." ".$rowCars['Model']." ".$rowCars['Age']." ".$rowCars['Reg']." ".$rowCars['Owner'];
echo "<img src=\"assets/images/" . $rowCars['Image'] . "\" />";
echo "<a href=\"editdata.php?theid=" . $rowCars['ID'] . "'\">Edit Car</a><br/>";

Whenever you are concatenating html and php, use " for avoiding this kind of issues and split it into either in different echo or store it in variable. " supports printing of strings as well as variables also and use backslash character \\ in describing string within another string like "src" attribute of "image" tag. Use ' to describe array index.
Hope you got it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM