php debugging, parsing commer value getting variable numbers
i have in database value that refers to ids of a another table it consists of numbers separates by' ,'
i want to get rid of the' ,' and get each différent numbers in variables
in my code i use
$mots_clefs = $row['mots_clefs'];
but i have
Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\citations\affiche_citations.php on line 12
what can i do?
please help
CREATE TABLE IF NOT EXISTS `citations` ( `id` int(11) unsigned NOT NULL, `auteur` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `texte` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `mots_clefs` varchar(20) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;---- Contenu de la table `citations`--INSERT INTO `citations` (`id`, `auteur`, `texte`, `mots_clefs`) VALUES(1, '1', 'À elle seule,La vie est une citation.', '10,11'),(0, '1', 'À elle seule,La vie est une citation.', '37,38,39');
$mots_clefs = $row['mots_clefs'];
This means $mots_clefs is a string, not an array. implode expects an array.
I don't really see where you use $mots_clefs2. You set it as an erroneous value, then forget about it.
PS: Ray will be here momentarily to tell you to switch from mysql to mysqli or PDO.
HTH,
Dan
0
jerrrrryAuthor Commented:
so, what can i write instead?
thanks
0
Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.
This should work, but it's ugly and screams for refactoring. Plus it's missing the connection to the keywords table, you're only outputting the id's of the keywords.
$query1 = "SELECT * FROM citations";$result1 = mysql_query($query1) or die(mysql_error());while($row = mysql_fetch_array($result1)) { $auteur_id=$row['auteur']; $mots_clefs = $row['mots_clefs']; $query2 = "SELECT NOM FROM auteurs where id = $auteur_id"; $result2 = mysql_query($query2) or die(mysql_error()); $row2 = mysql_fetch_array($result2); $auteur= $row2[0]; $texte = $row['texte']; echo $texte ; echo "<strong>"; echo $auteur; echo "</strong>"; echo $mots_clefs; echo"</br>";}
Good news. Even if you are new to PHP, you can still look up every PHP function on the PHP.net web site. Examples include these functions. You must read and understand this stuff if you want to write PHP code, no excuses. If your script spits out a message, the first line of defense is to look up the function and make sure your script is using it in a way that makes sense. http://php.net/implode http://php.net/explode
Really, you gotta look this stuff up and try to get an understanding. Here is a code fragment from an earlier post annotated with comments.
/** * IN THE FOLLOWING INSTRUCTION WE WILL TAKE A LITERAL STRING * AND ASSIGN IT TO A VARIABLE NAMED $pizza */$pizza = "piece1,piece2,piece3,piece4,piece5,piece6";/** * IN THE FOLLOWING INSTRUCTION WE WILL TAKE AN UNDEFINED VARIABLE * AND USE IT AS AN ARGUMENT TO A FUNCTION CALL. $pizza IS NOT THE SAME * VARIABLE AS $mots_clefs AND $mots_clefs IS UNDEFINED, SO explode() WILL * ISSUE A NOTICE AND THE VALUE ASSIGNED TO $pieces WILL BE MEANINGLESS */ $pieces = explode(",", $mots_clefs);
$query2 = "SELECT Nom FROM mots_clefs WHERE id IN ($mots_clefs_ids)";
This nom is not capitalized:
$mots_clefs[] = $row2['nom'];
In PHP, variables are case-sensitive. So are table and column names in MySQL. These are the sorts of things that you'll learn when you start some structured study of PHP and MySQL. You will be amazed how quickly the stumbling blocks can turn into stepping stones as you start the practice exercises in a well-developed curriculum.
Anyway, we seem to be wandering now. Have we answered your original question?
0
jerrrrryAuthor Commented:
no you have not answered yet the question, because i don't have the correct answer
i should have¿ elle seule,La vie est une citation. Jorge Luis BORGES keyword1 keyword2
¿ elle seule,La vie est une citation. Jorge Luis BORGES test1 test2
i have
¿ elle seule,La vie est une citation.1Array
¿ elle seule,La vie est une citation.1Array
i know there is accent issue we talked about with latin characters, not a problem here now i ll fix it later
Hopefully by now you understand why you do not want more than one data element in any column. All of this is unnecessary and confusing if the data base tables are designed correctly.
Here is an annotated bit of code explaining line-by-line what's going on. I'll sign off now. Best of luck with your project and please, please give yourself some much-needed quality time with that book!
/** * Using the while() iterator, retrieve data from the $result2 resource * with the fetch_array() function (which retrieves twice as much data as * you need, making it the least efficient way to retrieve the data). A * better choice would be fetch_object(). * * Each iteration will load data into an array named $row2, then the next * instruction will be executed, inside the control structure. */while ($row2 = mysql_fetch_array($result2)) {/** * As each $row2 array is created, go into the array and find the value * of the variable at the index named 'nom' and copy this value into an * array named $mots_clefs. Because of the empty square bracket notation * after $mots_clefs PHP knows (1) this is an array and (2) the variable * from $row['nom'] is to be appended to the array in a new numbered element. */ $mots_clefs[] = $row2['nom'];/** * This is the end of the control structure created by the while() iterator. * The semicolon is superfluous and should be removed to avoid confusion. */};/** * This echo statement writes the contents of three variables and some * string literals to the browser output stream. Because some of the * variables are strings the contents of the variable will be sent. But * PHP does not know what you want to print when you use an array with * echo, so it just prints the word "Array" and ignores the data. */echo "$texte<strong>$auteur</strong>$mots_clefs<br />";/** * This is likely what you want just before the echo statement. It will * take the $mots_clefs array and turn it into a string variable, with each * of the elements of the array separated by commas in the resulting string. * PHP will overwrite the $mots_clefs array variable with the new string. */$mots_clefs = implode(',', $mots_clefs);
Open in new window