PHP Tutorial (& MySQL) #28 - The Explode Function

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

Explains how to use the Explode function to turn a comma-separated list into an array in PHP

Full Transcript

all right then gangs so we're doing pretty well we've grabbed all the data from the database that we need the different pizzas and we're outputting them right here but one thing I'm not happy with is how we're outputting these different ingredients right here currently they're just in one single div tag and it's just one string of text now ideally what I like to do is a u tag and then a separate Li tag for each individual ingredient now we need to find a way to do this we need to find a way to cycle through the ingredients and output a bit of template and Li tag for each individual one now when I start to talk about cycling through data in my head I'm thinking arrays because that's what we do right we cycle through arrays we've done it for the pizzas so why not do it for the ingredients so it would be nice if we could convert this thing right here into an array of ingredients so how are we going to do that well fortunately in PHP it's really easy to do this kind of thing we're going to use a function called explode and that function is going to take in two arguments the first argument is going to be some kind of character that we want that function to look at and base how it's going to explode this string into an array on so for example we're going to say okay whenever you see a comma sign then I want you to take whatever's before that and put it in its own pocket its own position in the array so in this case it's going to look at this and say okay well this thing right here this is before the comma so I'm going to put this in its own position at the start in the array then this thing right here this is going to be the second position because after that comma it carries on reading and it comes across this comma so this thing the cheese is going to be put in the second position in the array and then finally it finishes reading mushroom and know there's no comma after it but this is All That Remains inside the string so it puts that text in the final position in the array okay so we're going to tell the explode function that this is how we want to split up this string into an array so that's the first argument we're going to pass in to the explode function the second argument is just the string that we want to explode which is this make sense so let's do this I'm going to do it up here in the code first of all so we can demo it and then what I'm going to do is output it down here in the actual temp template okay so quick example we say explode and the first parameter in quotes is going to be the comma that's how we want to split and make sure you spell this correctly explode not explode all right so that's the first parameter a comma sign that's how we're splitting the string the second parameter is going to be the actual string we want to explode and in this case what is it going to be well it's not just pizzas right because that that's already an array of pizzas we want a certain Pizza first of all so let's just grab the first one and that's position zero but then we want the ingredients from that pizza so we can say square brackets again to get the ingredients okay so that ingredients property right there that is the string that is ultimately being output right here remember we output that here okay so we cycle through the pizzas we grab that individual pizza and we output the ingredients we need to do zero here because we're not cycling through them here we're just grabbing one of the pieces so this should now give us some kind of array so I'm going to print this to the screen using print R print readable because that's how we print arrays to the screen and I'm going to save it and refresh over here so hopefully we should see those at the top now and now we can see this array zero tomato one cheese and two tofu so it's this thing right here cool so that's worked so this thing here if we take off that and take off that oops this thing is giving us an array now so this is the thing the array that we're going to cycle through for each pizza for each Pizza ingredients make sense so let's just comment this out and let's go down here so instead of outputting the ingredients like that I'm going to delete that and this is where we're going to cycle through them so I'd like to Output An Li tag for each individual ingredient so I'm going to do a UL first of all and then I'm going to do my for each Loop so PHP tags and four each now what is the array we want to cycle through well it's going to be explode then we're going to take the first argument which is the comma then the second argument which is going to be the individual Pizza we're currently cycling through and we want those ingredients so this here this is the array that we're cycling through much like we're cycling through the pizzas here cycling through this array right here that the explode function returns to us so that's what we're cycling through oops we need to say as and each time around we'll refer to each one as an ingredient or just ing so we'll say ing like so okay so we open up that code block we need to close this off down here so let's do another PHP tag and close that off now inside we want to Output An Li tag and that Li tag is going to contain the ingredient this thing over here okay so we're cycling through these we're turning this into an array cycling through them and we're going to Output an individual Li tag for each one so it be like cheese tomato mushroom Etc so let's just Echo out the ingredients so PHP tags Echo and we want to Echo the ingredient but remember a user has entered this don't trust them so use HTML special chars and then inside that we output the ingredient iron perfect so this is where we cross our fingers and hope that this has worked if we save and refresh now we can see each one of these has its own Li tag they're displayed differently make sense awesome so that to me looks a bit better that's how we can take a string and explode it into an array then we can cycle through that array

Original Description

Hey, in this PHP we'll take a look at the explode function - and how we can use it to turn our comma separated list of ingredients into an array of ingredients instead. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: + Course files - https://github.com/iamshaunjp/php-mysql-tutorial + VS Code editor - https://code.visualstudio.com/ + Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff 🤑🤑 Donate + https://www.paypal.me/thenetninja 🎓🎓 Find me on Udemy + https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →