Jump to content

Write "str_replace" before it's in the link


spacepoet

Recommended Posts

Hello:

 

I wanted to see if someone knows how to do a "str_replace" before it's in the link ..

 

This is the code I currently have and it does work, but I want the "str_replace" to take place before it gets added to the link:

<?php

$query  = "SELECT product_id,myTitle FROM myProducts ORDER BY myTitle ASC";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{				
$product_id = stripslashes($row['product_id']);
$myTitle = stripslashes($row['myTitle']);

?>

<li><a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo str_replace(" ", "-", $myTitle) ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a></li>

<?php 
} 
?>

 

Is there anyway I can make it happen here"

$myTitle = stripslashes($row['myTitle']);

 

Everything I have tried has not worked ..

 

Ideas?

Link to comment
Share on other sites

Just move the call to where you want it and save the result to a new variable, then put the variable in the link where the original call was.

 

$myTitle = stripslashes($row['myTitle']);
$myTitleDashed = str_replace(" ", "-", $myTitle);

 

<li><a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo $myTitleDashed; ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a></li>

Link to comment
Share on other sites

Hi:

 

Thank you -worked perfectly!

 

However, it did not solve my mod_rewrite issue (I thought it would).

 

Any idea why this:

#RewriteRule ^Promotional.Products-Promotional.Items/Promotional-Products/([0-9]+)-([a-zA-Z]+)-([a-zA-Z]+)-Promotional-Products.html$ Promotional.Products-Promotional.Items/Promotional-Products.php?product_id=$1&myTitleDashed=$2&full_state=$3

 

is not working? It keeps going to my 404 page.

 

 

It works fine when I am doing:

RewriteRule ^Promotional.Products-Promotional.Items/Promotional-Products/([0-9]+)-([a-zA-Z]+)-Promotional-Products.html$ Promotional.Products-Promotional.Items/Promotional-Products.php?product_id=$1&full_state=$2

<?php

$query  = "SELECT product_id,myTitle FROM myProducts ORDER BY myTitle ASC";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{				
$product_id = stripslashes($row['product_id']);
$myTitle = stripslashes($row['myTitle']);

?>

<li><a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a></li>

<?php 
} 
?>

 

But when I add the product to it, it keeps failing. It has something to do with the spaces in the product name, because when there is a product with a single word, it goes to the page fine.

 

I thought if I replaced the spaces with hyphens, it would match and I would not have any errors.

 

Any ideas??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.