Stringhe duplicate

Alex_70

Utente Attivo
13 Nov 2018
371
14
18
HELL
Buongiorno a tutti,

tramite questa query riesco a prelevare i dati nel campo genre ( varchar(40) ) , il problema e che sono duplicati,
quello che vorrei ottenere e' questo nella riga Tags

da cosi
Horror Horror Action Crime Drama

a cosi
Horror Action Crime Drama


sshot-1.png


codice

PHP:
<p class="bioheading">Tags</p><p class="biodata"></p>
<?php
            require_once("connetti.php");

$actor_id = $_GET['id'];

$query = "SELECT
genre
FROM film_actor
INNER JOIN film ON film_actor.film_id = film.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
WHERE film_actor.actor_id = $actor_id
";

$result = mysql_query( $query );
if (!$result)
die("mySQL error: ". mysql_error());
while( $row = mysql_fetch_object( $result ) ) : ?>





<?php

echo '<font color="green">' . $row->genre . '&nbsp;' . '</font>';

?>


Grazie
 
Ciao Tommy :)
ho inserito GROUP BY genre ma..
funziona solo su testo singolo, su doppio no, mi spiego
se e' cosi
Horror
e' OK
se invece e' cosi Horror Drama
visualizzo duplicato
le stringhe sono con lo spazio

sshot-2.png
 
Prova ad eseguire questa query su phpmyadmin e posta il risultato (cosi capisco meglio):
PHP:
SELECT
genre
FROM film_actor
INNER JOIN film ON film_actor.film_id = film.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
WHERE film_actor.actor_id = $actor_id GROUP BY genre
 
Ah ok pensavo che non ce ne fossero sulla stessa riga... Allora puoi fare così:
PHP:
<?php
            require_once("connetti.php");

$actor_id = $_GET['id'];

$query = "SELECT
genre
FROM film_actor
INNER JOIN film ON film_actor.film_id = film.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
WHERE film_actor.actor_id = $actor_id
";
$arr = array();
$result = mysql_query( $query );
if (!$result)
die("mySQL error: ". mysql_error());
while( $row = mysql_fetch_object( $result ) ) : ?>





<?php

$result = explode(" ",$row->genre);
foreach($result as $res){
array_push($arr, $res);
}


?>
// poi fuori dal while fai cosi:
<?php
foreach(array_unique($arr) as $ok){
echo "$ok ";
}
?>
 
ricevo errore

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in E:\OpenServer\domains\localhost\cinema\members\cinema.php on line 1583

line 1583

PHP:
while( $row = mysql_fetch_object( $result ) ) : ?>


sshot-1.png




PHP:
<p class="bioheading">Tags</p><p class="biodata"></p>


<?php
            require_once("connetti.php");

$actor_id = $_GET['id'];

$query = "SELECT
genre
FROM film_actor
INNER JOIN film ON film_actor.film_id = film.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
WHERE film_actor.actor_id = $actor_id
";
$arr = array();
$result = mysql_query( $query );
if (!$result)
die("mySQL error: ". mysql_error());
while( $row = mysql_fetch_object( $result ) ) : ?>





<?php

$result = explode(" ",$row->genre);
foreach($result as $res){
array_push($arr, $res);
}


            
            
?>

<?php // poi fuori dal while fai cosi:
foreach(array_unique($arr) as $ok){
echo "$ok ";
}
?>           
            
<? endwhile; ?>
 
Quindi diventa cosi:
PHP:
<p class="bioheading">Tags</p><p class="biodata"></p>


<?php
            require_once("connetti.php");

$actor_id = $_GET['id'];

$query = "SELECT
genre
FROM film_actor
INNER JOIN film ON film_actor.film_id = film.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
WHERE film_actor.actor_id = $actor_id
";
$arr = array();
$result = mysql_query( $query );
if (!$result)
die("mySQL error: ". mysql_error());
while( $row = mysql_fetch_object( $result ) ) : ?>





<?php

$risultato = explode(" ",$row->genre);
foreach($risultato as $res){
array_push($arr, $res);
}


            
            
?>
<? endwhile; ?>
<?php // poi fuori dal while fai cosi:
foreach(array_unique($arr) as $ok){
echo "$ok ";
}
?>
 
  • Like
Reactions: Alex_70
adesso funziona, :), grazie Tommy

si puo' fare in modo che se io clicco sul tag mi trova tutti i film con quel tag?
e' possibile? :rolleyes: e come?
 
Cambia il secondo e terzo $result in un nome di variabile diverso da $result che è l'oggetto mysql.
$new_result.
Se il secondo foreach deve effettivamente essere iterato al di fuori del while devi inserirlo dopo endwhile;
 
Cambia il secondo e terzo $result in un nome di variabile diverso da $result che è l'oggetto mysql.
$new_result.
Se il secondo foreach deve effettivamente essere iterato al di fuori del while devi inserirlo dopo endwhile;

cioe' :rolleyes:

Al posto di questo metti un link:
PHP:
<a href=" tag.php?tag=$ok">$ok</a>
Poi crei una pagina tag.php dove recuperi con GET il tag, fai una query per cercare i film con quel tag e li mostri


ok, provo :)
grazie ancora
 
la query come dovrebbe essere strutturata

cosi va bene?


PHP:
<?php


require_once("connetti.php");

$ok = $_GET["id"];
$query = mysql_query ("SELECT * FROM film_actor  WHERE genre=". $ok);

$row = mysql_fetch_array ($query);

?>
 
Penso di si, fai qualche prova.
Ah cmq se nel link hai messo tag.php?tag=$ok, su tag.php devi mettere $_GET['tag'] non $_GET['id'] (magari hai messo nel link tag.php?id=$ok e mi sbaglio)
 
Al posto di questo metti un link:
PHP:
echo "<a href=" tag.php?tag=$ok">$ok</a>";

Parse error: syntax error, unexpected 'tag' (T_STRING), expecting ',' or ';' in E:\OpenServer\domains\localhost\cinema\members\cinema.php on line 1605
 
Ultima modifica:
ho creato la pagina tag.php, il risultato non e' visibile,


sshot-1.png


penso che il motivo risiede nelle stringhe dentro il campo genre :rolleyes:

codice


PHP:
<?php
  
    require_once("connetti.php");
  

    $ok = $_GET["tag"];
$query = mysql_query ("SELECT * FROM film_actor  WHERE genre=". $ok);

$result = mysql_query( $query );
if (!$result)
die("mySQL error: ". mysql_error());
while( $row = mysql_fetch_object( $result ) ) : ?>



<tr>
  
<td><a href="film.php?id=<?php echo $row->film_id; ?>"></a>

<td class="text-left"><?php echo $row->genre; ?></td>

</tr>


<? endwhile; ?>
 

Discussioni simili