How to use str_replace with new mysqli oop code

Hi all

I’ve recently made the switch from mysql to mysqli and using a few new PHP coding techniques. Still not quite sure whats going on, though coming along.

The main issue right now is, below example to illustrate:

$resultVenues = $mysqli->query($queryVenues);
$venueArray = array();
while ($rowVenues = $resultVenues->fetch_assoc()) {
  $rowVenues['url'] = str_replace(" ", "-", (strtolower(ucwords($rowVenues['venue_id']))));
  $venueArray[] = $rowVenues['venue_id'];
}

And further down the page:

foreach($venueArray as $value){
  echo "<li><a href='venue-db/{$value}'>{$value}</a></li>";
}

How do I add the $url part so it replaces the first $value? Is the str_replace even right?
echo "<li><a href='venue-db/{$url}'>{$value}</a></li>";

And what is the difference between using:
{$value} and $venueArray[‘value’] ?

Is {$value} a OOP thing, I would usually use $row[‘someValue’]

Thanks, Barry

Your code is confusing as the example has two {$value} variables.

The variable in the brackets is for variable interpolation in the text (scroll down to “Complex (curly) syntax”). It has nothing to do with OOP. It’s simply a placeholder for the variable within the text string. So theoretically, it should look like this.

foreach($venueArray as $value){
  echo "<li><a href='venue-db/{$value['id']}'>{$value['url']}</a></li>";
}

However, the above code doesn’t properly build the $venueArray array. That needs fixing too.

Scott

I may be missing something here, but why use strtolower(ucwords())? What does ucwords() add to the party?

Yes, I need the same value for the link and the anchor text, though I need to add - in place of the spaces for the url, else page not found.

Example:
<a href="the-venue-name">The Venue Name</a>

So theoretically, it should look like this

Ok, I added {$value['url']} but now only the first character of the value is printed?

Example:
<a href="t">The Venue Name</a>

However, the above code doesn’t properly build the $venueArray array. That needs fixing too.

How? What is wrong? I thought this was correct. Things seem to work, its only when I try and add the str_replace into the mix.

Full code:
> $queryVenues = "

      SELECT venue_id 
      FROM tbl_venues
      WHERE active = 1
      AND venue_active = 1
      ORDER BY venue_id";
    $resultVenues = $mysqli->query($queryVenues);
//create array to hold the venue names
$venueArray = array();
while ($rowVenues = $resultVenues->fetch_assoc()) {
  $rowVenues['url'] = str_replace(" ", "-", (strtolower($rowVenues['venue_id'])));
  $venueArray[] = $rowVenues['venue_id'];
} 

foreach($venueArray as $value){
  echo "<li><a href='venue-db/{$value['url']}'>{$value}</a></li>";
}

but why use strtolower(ucwords())? What does ucwords() add to the party?

Cheers, yes you’re right, I copied this from any older snippet, updated. Good catch!

Also wondering…
Is there much difference if this was OOP?

Barry

As you’re building $venueArray[] like this:

  $venueArray[] = $rowVenues['venue_id'];

I can’t see how it would find a value for $value['url'] within the foreach() loop, and I can’t see why it doesn’t give an ‘undefined index’ error for that reference. What I could see working might be

  $venueArray[] = $rowVenues;
...
...
  echo "<li><a href='venue-db/{$value['url']}'>{$value['venue_id']}</a></li>";
1 Like

Perfect :grinning:

That works great droopsnoot, the reason I had things this way was because I only needed the venue_id at first, hence me using $venueArray[] = $rowVenues['venue_id'];

It makes a bit more sense now,
Thanks a lot!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.