I need to get a file name from a jpg without the extension to use as id in a span tag. Here's the code I'm using:

<?php
  $result = mysql_query("SELECT * FROM $data_sales_earrings ORDER BY id ASC");
  while($row = mysql_fetch_array($result))
  echo "
<span id=\"need file name from jpg front\"><img src=\"image/sale/".$row['image_item_front']."\" width=\"326\" height=\"435\" /></span>
<span id=\"need file name from jpg back\"><img src=\"image/sale/".$row['image_item_back']."\" width=\"326\" height=\"435\" /></span>

";
?>

The jpgs are image_item_front & 'image_item_back. I know how to strip the extension, but can't get it into the array.

Dani AI

Generated

Nice fix from — since the filename extraction is working for , add a couple of production-ready steps so the ID you emit is safe, predictable and unique.

Keep IDs CSS/JS-friendly and free of whitespace. Replace anything that is not a letter, digit, underscore or hyphen with a single hyphen, collapse runs of hyphens and trim them off the ends. Also avoid IDs that start with a digit (they are valid HTML but require CSS escaping), so prefix when needed. Example sanitization:

// $filename = base filename without extension (obtained earlier)
$safe = preg_replace('/[^A-Za-z0-9_-]+/', '-', $filename);
$safe = preg_replace('/-+/', '-', $safe);
$safe = trim($safe, '-');
if (preg_match('/^[0-9]/', $safe)) {
    $safe = 'img-' . $safe; // avoid leading digit
}

An element ID should also be unique on the page — append the DB row id and a side suffix (front/back) so front/back images and different rows never collide, and always escape the final attribute value when printing to HTML to prevent accidental markup injection:

$id_attr = 'item-' . (int)$row['id'] . '-front-' . $safe;
echo '<span id="' . htmlspecialchars($id_attr, ENT_QUOTES, 'UTF-8') . '"> ... </span>';

Use htmlspecialchars() when outputting attribute values to HTML. Also consider moving away from the old mysql_* API used in the thread: those functions were deprecated and later removed from PHP; modern code should use MySQLi or PDO with prepared statements for safety and forward compatibility. For quick debugging of parse errors, check matching braces and missing semicolons first — the common culprits. (developer.mozilla.org)

Recommended Answers

All 6 Replies

<?php
  $result = mysql_query("SELECT * FROM $data_sales_earrings ORDER BY id ASC");
  while($row = mysql_fetch_array($result))
		$path_parts = $row['image_item_front'];
		$id_front = $path_parts['basename'], 
		$path_parts = $row['image_item_back'];
		$id_back = $path_parts['basename'], 
		echo "<span id=\"".$id_front."\"><img src=\"image/sale/".$row['image_item_front']."\" width=\"326\" height=\"435\" /></span>
                      <span id=\"".$id_back."\"><img src=\"image/sale/".$row['image_item_back']."\" width=\"326\" height=\"435\" /></span>";
	}
?>

This will get the name without extension put it in a variable and use that inside the id tag.

I get this error with this code:

Parse error: parse error in C:\wamp\www\Frank\sales-earrings-db.php on line 140, which is this line:

$id_front = $path_parts,

I get this error with this code:

Parse error: parse error in C:\wamp\www\Frank\sales-earrings-db.php on line 140, which is this line:

$id_front = $path_parts,

Sorry, my mistake. Sometimes I think to quick and press reply to quickly.
It should be:

$path_parts = pathinfo($row['image_item_front']);
$id_front = $path_parts['filename'];
$path_parts = pathinfo($row['image_item_back']);
$id_back = $path_parts['filename'];

basename gives the filename + extension.

Now I get this error:

Parse error: parse error in C:\wamp\www\Frank\sales-earrings-db.php on line 145

It's the last line, the last }

Now I get this error:

Parse error: parse error in C:\wamp\www\Frank\sales-earrings-db.php on line 145

It's the last line, a }

Overlooked your while loop. It should always be while .... { if there are more commands after the while. You only had an echo and I just added a } at the end, but didn't see there was no { at the start.

Duh! I forgot the brackets. It works now. Thanks loads!

Here's the code:

<?php
  $result = mysql_query("SELECT * FROM $data_sales_earrings ORDER BY id ASC");
  while($row = mysql_fetch_array($result)){
		        $path_parts = pathinfo($row['image_item_front']);
                $id_front = $path_parts['filename'];
                $path_parts = pathinfo($row['image_item_back']);
                $id_back = $path_parts['filename'];
		echo "<span id=\"".$id_front."\"><img src=\"image/sale/".$row['image_item_front']."\" width=\"326\" height=\"435\" /></span>
                      <span id=\"".$id_back."\"><img src=\"image/sale/".$row['image_item_back']."\" width=\"326\" height=\"435\" /></span>";
	}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.