Mysql explode string avoiding text within html tags

What was the intention here, to get information for just one film?

I think it may work if you use a foreach but without the while loop.
First get the info for the film using the first row, Eg:-

$filmData = mysqli_fetch_array($result) ;
echo "<h2>$filmData[0]['titolo']"; 

The things you only want to see once are not in a loop.

Then when you want to list the cast, use a foreach loop, Eg:-

foreach($fimlData as $cast) {
   echo "<span><a href=\"hashtag-actors.php?tag=$cast[cognome]\">$cast[nome] $cast[cognome]</a> (as $cast[role])</span>";
}
1 Like

Thank you!
This code works until foreach:

$result = mysqli_query($db, $query);
$filmData = mysqli_fetch_array($result);
echo "<h2>$filmData[0]"; 
if($filmData[2]!=''){echo "<img src=\"$filmdata[2]\" height=\"200px\" class=\"fr\" />";}
echo "</h2>";
echo "<p>data:<b>$filmData[3]</b> - regista: $filmData[5] <a href='hashtag-registi.php?tag=$filmData[4]'>$filmData[4]</a></p>";
echo "<blockquote><p>$filmData[6]</p></blockquote>";
echo "<p class='actors'>";
foreach($fimlData as $cast) {
   echo "<span><a href=\"hashtag-actors.php?tag=$cast[8]\">$cast[9] $cast[8]</a> (as $cast[10])</span>";
}

I have tried also with $cast[cognome]\">$cast[nome] $cast[cognome], as you suggested, but with the same result: no output (for that code rows).
I get now:

Good Will Hunting

data: 1997 - regista: Gus [Van Sant](http://localhost/cultura/cinema/hashtag-registi.php?tag=Van Sant)

My bad!
There is a typo in the foreach.

foreach($filmData as $cast) {

But why are you using different keys from the array?
It would be sensible to stick with the first [0] as the number of rows will be an unknown, but there is sure to be at least the first.

1 Like

I add that the expected result should be

Good Will Hunting

data: 1997 - regista: Gus Van Sant

Matt Damon (as Will Hunting (ragazzo geniale)) Robin Williams (as Sean Maguire (psicologo))

temi :condizione giovanile

Thanks, but even fixing this error, the result is quite similar.

Having looked more closely at your code, I see a few things wrong now. Mainly how you are trying to access array data.
It is a multi dimensional array, with rows each containing a sub-array. As you have seen, much of the data is duplicated and redundant. This is why I advise only to use the first row [0] to get the film data (which is duplicated in every row), then only use a loop to access the cast list from each row.
Assuming the data from the DB looks something like this:-

$filmData = [
	[
		'titolo' => 'Good Will Hunting',
		'imagelink' => 'img.jpg',
		'data' => '1997',
		'regia_nome' => 'Gus',
		'regia' => 'Van Sant',
		'contenuti' => 'Contents...',
		'nome' => 'Matt',
		'cognome' => 'Damon',
		'role' => 'Will Hunting (ragazzo geniale)'
	],
	[
		'titolo' => 'Good Will Hunting',
		'imagelink' => 'img.jpg',
		'data' => '1997',
		'regia_nome' => 'Gus',
		'regia' => 'Van Sant',
		'contenuti' => 'Contents...',
		'nome' => 'Robin',
		'cognome' => 'Williams',
		'role' => 'Sean Maguire (psicologo)'
	]
];

I had to make this to test the code, not having your database.

When accessing data from the first row of a multi dimensional array, you will first specify the row, then the data column: $array[0]['column']
Also take not of your quote marks, you need quotes for the column name.
When echoing a string in double quotes, you may insert a variable name without breaking out of the quotes, but when accessing a multi dimensional array like this, you will need to break out of the quotes, which can get messy.

$result = mysqli_query($db, $query);
$filmData = mysqli_fetch_array($result);

echo "<h2>".$filmData[0]['titolo'];  // Get the title from the first row
if($filmData[0]['imagelink'] != ''){echo '<img src="'.$filmData[0]['imagelink'].'" height="200px" class="fr">';}
echo "</h2>";
echo "<p>data:<b>".$filmData[0]['data']."</b> - regista: ".$filmData[0]['regia_nome']." <a href='hashtag-registi.php?tag=".$filmData[0]['regia']."'>".$filmData[0]['regia']."</a></p>";
echo "<blockquote><p>".$filmData[0]['contenuti']."</p></blockquote>"; // All this comes from the first row
echo "<p class='actors'>";
foreach($filmData as $cast) {   // Now take the cast data in a loop...
   echo "<span><a href=\"hashtag-actors.php?tag=".$cast['cognome']."\">".$cast['nome']." ".$cast['cognome']."</a> (as ".$cast['role'].")</span>";
}
1 Like

Thank you, very much. But unfortunately this new code get an empty page (totally empty) :face_with_diagonal_mouth: :thinking:

I copied it exactly. Maybe it is better I copy the whole php page. Here you are:

<?php
$db_host = "localhost";
$db_user = "myuser";
$db_password = "mypsw";
$db_name ="mydb";
$db = mysqli_connect($db_host, $db_user, $db_password, $db_name);
mysqli_set_charset($db, 'utf8');
$query = "
SELECT f.titolo, f.titolo_italiano, f.imagelink, f.data, f.regia, f.regia_nome, f.contenuti, f.key_libere, a.cognome, a.nome, c.role, c.mov_genre
FROM films__olon f
JOIN films__cast c
ON c.mov_genre=f.genere
JOIN films__actors a
ON a.act_id=c.act_id
WHERE c.mov_id=f.mov_id AND f.genere LIKE '%giovan%'
ORDER by DATA";

$result = mysqli_query($db, $query);
$filmData = mysqli_fetch_array($result);

echo "<h2>".$filmData[0]['titolo'];  // Get the title from the first row
if($filmData[0]['imagelink'] != ''){echo '<img src="'.$filmData[0]['imagelink'].'" height="200px" class="fr">';}
echo "</h2>";
echo "<p>data:<b>".$filmData[0]['data']."</b> - regista: ".$filmData[0]['regia_nome']." <a href='hashtag-registi.php?tag=".$filmData[0]['regia']."'>".$filmData[0]['regia']."</a></p>";
echo "<blockquote><p>".$filmData[0]['contenuti']."</p></blockquote>"; // All this comes from the first row
echo "<p class='actors'>";
foreach($filmData as $cast) {   // Now take the cast data in a loop...
   echo "<span><a href=\"hashtag-actors.php?tag=".$cast['cognome']."\">".$cast['nome']." ".$cast['cognome']."</a> (as ".$cast['role'].")</span>";
}
echo "</p>";
?>

Sorry…

It worked for me.
The difference being the references to the database are commented out. Instead I include the array definition to set the $filmData variable, since I don’t have your database.

Is there no error shown? Do you have error reporting enabled?

1 Like

Also to debug, you could temporarily add a var_dump($filmData) to check the structure and content of the data resembles something like my array.
I constructed that array on an assumption of what your data would be like.

1 Like

The output with var_dump($filmData) is

array(24) { [0]=> string(17) "Good Will Hunting" ["titolo"]=> string(17) "Good Will Hunting" [1]=> string(13) "Genio ribelle" ["titolo_italiano"]=> string(13) "Genio ribelle" [2]=> NULL ["imagelink"]=> NULL [3]=> string(4) "1997" ["data"]=> string(4) "1997" [4]=> string(8) "Van Sant" ["regia"]=> string(8) "Van Sant" [5]=> string(3) "Gus" ["regia_nome"]=> string(3) "Gus" [6]=> NULL ["contenuti"]=> NULL [7]=> string(26) "condizione giovanile,genio" ["key_libere"]=> string(26) "condizione giovanile,genio" [8]=> string(5) "Damon" ["cognome"]=> string(5) "Damon" [9]=> string(4) "Matt" ["nome"]=> string(4) "Matt" [10]=> string(30) "Will Hunting (ragazzo geniale)" ["role"]=> string(30) "Will Hunting (ragazzo geniale)" [11]=> string(9) "giovanile" ["mov_genre"]=> string(9) "giovanile" }

Thank you!

That array is different to what I expected. but I notice now your fetch mode:-

$filmData = mysqli_fetch_array($result);

Try another mode:-

$filmData = mysqli_fetch_all($result, MYSQLI_ASSOC);

Though I am very rusty with mysqli, I have not used it for years.

It is strange that your array says 24 but I only count 22 in it.
Reformatted for clarity:-

array(24) {
	[0]=> string(17) "Good Will Hunting"
	["titolo"]=> string(17) "Good Will Hunting"
	[1]=> string(13) "Genio ribelle"
	["titolo_italiano"]=> string(13) "Genio ribelle"
	[2]=> NULL ["imagelink"]=> NULL
	[3]=> string(4) "1997"
	["data"]=> string(4) "1997"
	[4]=> string(8) "Van Sant"
	["regia"]=> string(8) "Van Sant"
	[5]=> string(3) "Gus"
	["regia_nome"]=> string(3) "Gus"
	[6]=> NULL ["contenuti"]=> NULL
	[7]=> string(26) "condizione giovanile,genio"
	["key_libere"]=> string(26) "condizione giovanile,genio"
	[8]=> string(5) "Damon"
	["cognome"]=> string(5) "Damon"
	[9]=> string(4) "Matt"
	["nome"]=> string(4) "Matt"
	[10]=> string(30) "Will Hunting (ragazzo geniale)"
	["role"]=> string(30) "Will Hunting (ragazzo geniale)"
	[11]=> string(9) "giovanile"
	["mov_genre"]=> string(9) "giovanile"
}
1 Like

Great! This works!!!
Some other further attempts (widening the data) this afternoon.
Thank you very, very much!!!

EDIT

After some new attempts, it results this problem. The code works, but if there are several films only one is shown, and the actors are putting together.

Dead Poets Society

data:1989 - regista: Peter Weir

Robin Williams (as professor Keaton)Matt Damon (as Will Hunting (ragazzo geniale))Robin Williams (as Sean Maguire (psicologo))

This is the dump:

array(3) { 
[0]=> array(12) { 
["titolo"]=> string(18) "Dead Poets Society" 
["titolo_italiano"]=> string(17) "L'attimo fuggente" 
["imagelink"]=> NULL 
["data"]=> string(4) "1989" 
["regia"]=> string(4) "Weir" 
["regia_nome"]=> string(5) "Peter" 
["contenuti"]=> NULL 
["key_libere"]=> NULL 
["cognome"]=> string(8) "Williams" 
["nome"]=> string(5) "Robin" 
["role"]=> string(16) "professor Keating" 
["mov_genre"]=> string(9) "giovanile" 
} 
[1]=> array(12) { 
["titolo"]=> string(17) "Good Will Hunting" 
["titolo_italiano"]=> string(13) "Genio ribelle" 
["imagelink"]=> NULL 
["data"]=> string(4) "1997" 
["regia"]=> string(8) "Van Sant" 
["regia_nome"]=> string(3) "Gus" 
["contenuti"]=> NULL 
["key_libere"]=> string(26) "condizione giovanile,genio" 
["cognome"]=> string(5) "Damon" 
["nome"]=> string(4) "Matt" 
["role"]=> string(30) "Will Hunting (ragazzo geniale)" 
["mov_genre"]=> string(9) "giovanile" 
} 
[2]=> array(12) { 
["titolo"]=> string(17) "Good Will Hunting" 
["titolo_italiano"]=> string(13) "Genio ribelle" 
["imagelink"]=> NULL 
["data"]=> string(4) "1997" 
["regia"]=> string(8) "Van Sant" 
["regia_nome"]=> string(3) "Gus" 
["contenuti"]=> NULL 
["key_libere"]=> string(26) "condizione giovanile,genio" 
["cognome"]=> string(8) "Williams" 
["nome"]=> string(5) "Robin" 
["role"]=> string(24) "Sean Maguire (psicologo)" 
["mov_genre"]=> string(9) "giovanile" 
}
 }
1 Like

Maybe the EDIT way hides my last message, doesn’t it?
If so, I repeat here (not to hurry anybody, but to be sure my message be read):

"After some new attempts, it results this problem. The code works, but if there are several films only one is shown, and the actors are putting together.

Dead Poets Society

data:1989 - regista: Peter Weir

Robin Williams (as professor Keating)Matt Damon (as Will Hunting (ragazzo geniale))Robin Williams (as Sean Maguire (psicologo))

This is the dump :

array(3) { 
[0]=> array(12) { 
["titolo"]=> string(18) "Dead Poets Society" 
["titolo_italiano"]=> string(17) "L'attimo fuggente" 
["imagelink"]=> NULL 
["data"]=> string(4) "1989" 
["regia"]=> string(4) "Weir" 
["regia_nome"]=> string(5) "Peter" 
["contenuti"]=> NULL 
["key_libere"]=> NULL 
["cognome"]=> string(8) "Williams" 
["nome"]=> string(5) "Robin" 
["role"]=> string(16) "professor Keating" 
["mov_genre"]=> string(9) "giovanile" 
} 
[1]=> array(12) { 
["titolo"]=> string(17) "Good Will Hunting" 
["titolo_italiano"]=> string(13) "Genio ribelle" 
["imagelink"]=> NULL 
["data"]=> string(4) "1997" 
["regia"]=> string(8) "Van Sant" 
["regia_nome"]=> string(3) "Gus" 
["contenuti"]=> NULL 
["key_libere"]=> string(26) "condizione giovanile,genio" 
["cognome"]=> string(5) "Damon" 
["nome"]=> string(4) "Matt" 
["role"]=> string(30) "Will Hunting (ragazzo geniale)" 
["mov_genre"]=> string(9) "giovanile" 
} 
[2]=> array(12) { 
["titolo"]=> string(17) "Good Will Hunting" 
["titolo_italiano"]=> string(13) "Genio ribelle" 
["imagelink"]=> NULL 
["data"]=> string(4) "1997" 
["regia"]=> string(8) "Van Sant" 
["regia_nome"]=> string(3) "Gus" 
["contenuti"]=> NULL 
["key_libere"]=> string(26) "condizione giovanile,genio" 
["cognome"]=> string(8) "Williams" 
["nome"]=> string(5) "Robin" 
["role"]=> string(24) "Sean Maguire (psicologo)" 
["mov_genre"]=> string(9) "giovanile" 
}
 }

Thank you!

Maybe try with PDO?

So to clarify your intention. You want your page to list a number of films and list each films cast in the same page?
I was thinking that you were only showing one film with its cast on a page at a time.

If it is multiple films with cast, you may need more than one query to do that. A query to get the list of films, and another to get the cast for each one.
This will mean running the cast query several times, once for each film, but that can be made more efficient with a prepared statement.

PDO is always my preference.

1 Like

Thank you, Sam.
Yes I wish, if possible

Indeed I have already a php page doing that, but the problem is, as I said in the first post of this thread, that I was not able to show the role of an actor in a particular film.
Therefore, what I’m searching now is to replace that php page (coming from an only mysql table), with a new one, from different mysql tables. Following your statement about “bad database design”.
So, I will be very grateful if you help me to do this task (with PDO or not, for me it’s the same).

I have given enough data or I should give further code/data?
Thank you!

The current database schema would help.

1 Like

I am not sure about what do you mean by “current database schema”.
In my present msqli php page I have this code, after calling server,user,psw and db name (that is $db variable):

$query = "SELECT * FROM films__olon ORDER by data";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_array($result)){ 
if(empty($row['titolo'])){echo "<h2>$row[titolo_italiano]";} else{echo "<h2 title=\"$row[titolo_italiano]\">$row[titolo]";}
if(empty($row['imagelink'])){echo "";} else{echo "<img src=\"$row[imagelink]\" height=\"200px\" class=\"fr\" />";}
echo "</h2>";
echo "<p>data:<b>$row[data]</b> - regista: $row[regia_nome] <a href=\"hashtag-registi.php?tag=$row[regia]\">$row[regia]</a></p>";
echo "<blockquote lang=\""."$row[lingua]\"><p>"."$row[contenuti]</p></blockquote>";
echo "<p class=\"actors\">";
$keywordsa = $row['attori'];
if(empty($row["attori"])){echo "";} else{echo "<p class=\"keywords\"><b>attori</b>:";}
foreach (explode(',', $keywordsa) as $keya) {
    if(empty($row["attori"])){echo "";} else{echo "<span><a href=\"hashtag-actors.php?tag=$keya\">{$keya}</a></span>";}
}
echo "</p>";
$keywords = $row['key_libere'];
if(empty($row['key_libere'])){echo "";} else{echo "<p class=\"keywords\"><b>temi</b>:";}
foreach (explode(',', $keywords) as $key) {
    if(empty($row['key_libere'])){echo "";} else{echo "<span><a href=\"hashtag-films.php?tag=$key\">{$key}</a></span>";}
}
echo "</p>";}

In this way I have a working php page, with hashatgs for director (“regia”), actors and keywords (“key_libere”).
Thank you!

He means an SQL dump of your Database along with sample data.

2 Likes