Replace Text Character with Image

Is it possible, using php, to replace a character with a specific image ?

So where the character 9 appears, display 9.png instead ?

I am aware of methods using javascript but my number is dynamically generated and changes using jquery so the java methods available don’t seem to apply themselves once the number updates.

Any ideas ?

Try using str_replace() as long as the initial string is catchable during processing on the server side.

Thanks for your reply, but i’m really not sure where to start, or if i explained correctly basically the html below is how i show the COUNT from my database, and that count changes using ajax, to display the COUNT based on filters.

<div class="stock"><span id='dynamicStockCount'></span>&nbsp; Vehicles in stock</div> <span id='originalCount' style='display:none'><?php echo $resultCount;?></span>

So basically $resultCount will always be a number (with commas) like 9,100,356 Vehicles in stock

But i would like (somehow) to display 9.gif comma.gif 1.gif 0.gif etc etc

Can you point me in the direction of an example of how i could use str_replace() for this method ?

Thanks again for your help !

Try this:

[php]

<?php $strNumber = "9,100,356"; for ($intCount = 0; $intCount <= strlen($strNumber); $intCount++){ echo ""; } ?>

[/php]

That should be:

[php]<?php
$strNumber = “9,100,356”;
for ($intCount = 0; $intCount < strlen($strNumber); $intCount++){

echo “<img src=“images/” . substr($strNumber, $intCount, 1) . “.gif”>”;
}
?>[/php]

It will output this:

xerxes, that seems to work perfectly but how would i tie it in with the code i posted to apply it to

<?php echo $resultCount;?>

Replace $strNumber with $resultCount, I’ve also used strVal, although I’m not sure it’s absolutely necessary.

[php]<?php
$resultCount = strVal($resultCount);
for ($intCount = 0; $intCount < strlen($resultCount); $intCount++){
echo “<img src=“images/” . substr($resultCount, $intCount, 1) . “.gif”>”;
}
?>[/php]

Hey thanks again, i got it to function, but as with java options it doesn’t stay once the number changes, i guess it would need tied into the java or require a page re-load.

But it was worth a try !

Thanks again

What’s the rest of the page doing? Let’s see that code.

Sure … it really consists of 2 pages form.php & get_models.php here they are

Form.php

[code]

<?php require_once 'config.php'; // for database connection to be removed later $query = "SELECT `make` , COUNT(`make`) AS makeCount FROM vehicles GROUP BY `make` ORDER BY `make` ASC;"; $result = mysql_query($query); $resultCount =0; $arrMake= array(); if($result){ while($row = mysql_fetch_assoc($result)){ $resultCount = $resultCount + $row['makeCount']; if($resultCount){ $make = array( 'make' => $row['make'], 'makeCount' => $row['makeCount'], ); //$arrMake[] = $row['make'].' ('.$row['makeCount'].')'; $arrMake[] = $make; } } //print_r($arrMake); } ?>
<fieldset>
	<div class="stock"><span id='dynamicStockCount'></span>&nbsp; Vehicles in stock</div>
	<span id='originalCount' style='display:none'><?php echo $resultCount;?></span>
</fieldset>
<fieldset>
	<label>Manufacturer:</label>
  
	<select name="make" id="drop_1" style="width:260px">
		<option value="" id="" selected="selected" disabled="disabled">Make</option>
		<?php foreach($arrMake AS $make){

		?>
			<option value="<?php echo $make['make']?>" id='<?php echo $make['makeCount'];?>' >
				<?php echo $make['make'].' ('.$make['makeCount'].')';?>
			</option>
		<?php
		}
		?>
	</select> 


</fieldset>
<fieldset>
           <label>Model:</label>
	<span style="display: inline;" id="result_1">
		
                            <span id="wait_1" style="display: none;">
				<img alt="Please Wait" src="images/ajax-loader.gif">
			</span>
			
			<select name="model" id="tier_two" style="width:260px">
				<option value=" " disabled="disabled" selected="selected">Choose one</option>
				<!-- <option value="FIESTA HATCHBACK">FIESTA HATCHBACK</option> -->
			</select>
		
	</span>
			

</fieldset>		        				
<fieldset>

	<label>Min Price:</label>
	<select id="minPrice" name="minPrice">
		<option value="0">No min</option>
		<option value="500">&pound;500</option>
		<option value="1000">&pound;1,000</option>
		<option value="2000">&pound;2,000</option>
		<option value="3000">&pound;3,000</option>
		<option value="4000">&pound;4,000</option>
		<option value="5000">&pound;5,000</option>
		<option value="6000">&pound;6,000</option>
		<option value="7000">&pound;7,000</option>
		<option value="8000">&pound;8,000</option>
		<option value="9000">&pound;9,000</option>
		<option value="10000">&pound;10,000</option>
		<option value="12000">&pound;12,500</option>
		<option value="15000">&pound;15,000</option>
		<option value="17000">&pound;17,500</option>
		<option value="20000">&pound;20,000</option>
		<option value="25000">&pound;25,000</option>
		<option value="30000">&pound;30,000</option>
		<option value="35000">&pound;35,000</option>
		<option value="40000">&pound;40,000</option>
		<option value="45000">&pound;45,000</option>
		<option value="50000">&pound;50,000</option>
		<option value="75000">&pound;75,000</option>
		<option value="100000">&pound;100,000</option>
		<option value="250000">&pound;250,000</option>
		<option value="500000">&pound;500,000</option>
	</select>
</fieldset>
			
<fieldset>
	<label>Max Price:</label>
	<select id="maxPrice" name="maxPrice">
		<option value="0">No max</option>
		<option value="500">&pound;500</option>
		<option value="1000">&pound;1,000</option>
		<option value="2000">&pound;2,000</option>
		<option value="3000">&pound;3,000</option>
		<option value="4000">&pound;4,000</option>
		<option value="5000">&pound;5,000</option>
		<option value="6000">&pound;6,000</option>
		<option value="7000">&pound;7,000</option>
		<option value="8000">&pound;8,000</option>
		<option value="9000">&pound;9,000</option>
		<option value="10000">&pound;10,000</option>
		<option value="12000">&pound;12,500</option>
		<option value="15000">&pound;15,000</option>
		<option value="17000">&pound;17,500</option>
		<option value="20000">&pound;20,000</option>
		<option value="25000">&pound;25,000</option>
		<option value="30000">&pound;30,000</option>
		<option value="35000">&pound;35,000</option>
		<option value="40000">&pound;40,000</option>
		<option value="45000">&pound;45,000</option>
		<option value="50000">&pound;50,000</option>
		<option value="75000">&pound;75,000</option>
		<option value="100000">&pound;100,000</option>
		<option value="250000">&pound;250,000</option>
		<option value="500000">&pound;500,000</option>
		<option value="1000000">&pound;1,000,000</option>
	</select>
</fieldset>
		
<div class="clear">
</div>
<button id="submitSearch" class="submit-search" type="submit">
	Search
</button>
[/code]

get_models.php

[code]<?php
header(‘content-type: application/json; charset=utf-8’);

require_once(‘config.php’);//for db connection to be removed later
$arrResponse = array();

if(isset($_REQUEST[‘make’])){
$make = $_REQUEST[‘make’];

$query = "SELECT `model` , COUNT(`model`) AS modelCount FROM vehicles  WHERE `make`= '$make' GROUP BY `model` ORDER BY `model` ASC;";
//echo $query;
$result = mysql_query($query);
$resultCount =0;
$arrModel= array();
if($result){
	while($row = mysql_fetch_assoc($result)){
		
		$resultCount = $resultCount + $row['modelCount'];
		if($resultCount){
			$model = array(
							'model' => $row['model'],
							'modelCount' => $row['modelCount'],
						);
			$arrModel[] = $model;
		}
	}
	$arrResponse['success'] = TRUE;
	$arrResponse['data'] =$arrModel ;
	
	
}
else{
	$arrResponse['success'] = FALSE;
}

}
else{
$arrResponse[‘success’] = FALSE;
}

echo json_encode($arrResponse);
?>[/code]

Hmmm,

Can’t quite see what’s going on, looks like you’ve got some dependant drop downs, but they look a bit more complicated than the method I used. Take a look at AdInsert.php and Includes/AjaxSubCategories.php that’s on the web space that you set up for me.

Yes that’s correct ! They are quite complicated because they are not only dependent on the values within the drop downs but also with the COUNT displayed, which is why i am having difficulty styling the count in the way i want.

I discovered lettering.js which breaks the number down, putting each character between it’s own tags but again once the number changes by ajax, the lettering.js doesn’t get applied .

It’s not really a huge problem … it’s just a styling issue which i can live without !!

Sponsor our Newsletter | Privacy Policy | Terms of Service