kez1985 Posted May 9, 2008 Share Posted May 9, 2008 Hi, I'm a novice when it comes to php and mysql and am having problems with a script I'm writing... THE CODE: function connect() { $dbname = "database"; $dbuser = "user"; $dbpass = "password"; $connection = @mysql_connect("localhost","$dbuser","$dbpass")or die(mysql_error()); $db = @mysql_select_db($dbname, $connection) or die(mysql_error()); return $connection; } $conn = connect(); $sql = 'SELECT * FROM `jos_menu` WHERE `menutype` = "lifestyle" OR `menutype` = "lifeStyle" OR `menutype` = "LifeStyle"'; $result = mysql_query($sql, $conn) or die(mysql_error()); $n = 0; while($row = mysql_fetch_object($result)) { echo "object $n"; $link[$n] = $row->link; $name[$n] = $row->name; echo $link[$n].""; echo $name[$n].""; echo "end object"; $n++; } echo "\$link has ".sizeof($link)." array members"; $sql = "SELECT * FROM `jos_content` WHERE `catid` = "; $catn = 0; while($n != sizeof($link)) { $word = "id="; $count = 0; while($count != sizeof($link[$n])) { $countend = $count+3; if(substr($link[$n][$count],$link[$n][$countend]) == $word) { echo "This is link $link[$n][$count]"; echo substr($link[$n][$count+4],strlen($link[$n])); $catid[$catn] = substr($link[$n][$count+4],strlen($link[$n])); echo "Category ID: ".$catid[$catn]; $catn++; } $count++; } $n++; } $sql .= " AND CHAR_LENGTH(introtext) < 30"; echo "$sql"; $result = mysql_query($sql, $conn) or die(mysql_error()); $n = 0; while ($row = mysql_fetch_object($result)) { echo "object $n"; $id[$n] = $row->id; $title[$n] = $row->title; $introtext[$n] = $row->introtext; $description[$n] = $title[$n]." - ".$introtext[$n]; $description[$n] = strip_tags($description[$n]); $newline = "\r\n"; $repchar = " "; str_replace($newline,$repchar,$description[$n]); echo $description[$n]; echo "end object"; $n++; } echo "total array elements: ".sizeof($description).""; $n = 0; while($n != sizeof($description)) { $sql = "UPDATE `jos_content` SET `metadesc` = \"$description[$n]\" WHERE id = \"$id[$n]\""; $result = mysql_query($sql, $conn) or die(mysql_error()); $n++; } mysql_free_result($result); The project is Joomla! based and what I'm doing is concatenating the title of the content item and intro text to create a meta description for the page, then injecting it back in. Most of the code is working and I have had a prototype work before with a different bit at the start. The part that I'm having problems with is stepping through the array of links in the menu table to pull out the ids of the menu items I want to change. Specifically I want to be able to find a substring of the link field (the value of id) and write that to a new array or remove the rest of the string from the array. The link looks like this: _|_ | | index.php?option=com_content&task=blogcategory&id=306 Any help for this would be amazingly helpfull as I am lost :-s Kerry Link to comment https://forums.phpfreaks.com/topic/104856-problems-getting-a-substring-of-a-mysql-field/ Share on other sites More sharing options...
Barand Posted May 9, 2008 Share Posted May 9, 2008 <?php $str = 'index.php?option=com_content&task=blogcategory&id=306'; parse_str($str); echo $id; // --> 306 Link to comment https://forums.phpfreaks.com/topic/104856-problems-getting-a-substring-of-a-mysql-field/#findComment-536886 Share on other sites More sharing options...
kez1985 Posted May 12, 2008 Author Share Posted May 12, 2008 thank you sensei, your wisdom is greatly respected and appreciated the fix is thus... function connect() { $dbname = "database"; $dbuser = "user"; $dbpass = "password"; $connection = @mysql_connect("localhost","$dbuser","$dbpass")or die(mysql_error()); $db = @mysql_select_db($dbname, $connection) or die(mysql_error()); return $connection; } $conn = connect(); $sql = 'SELECT * FROM `jos_menu` WHERE `menutype` = "lifestyle" OR `menutype` = "lifeStyle" OR `menutype` = "LifeStyle"'; $result = mysql_query($sql, $conn) or die(mysql_error()); $n = 0; while($row = mysql_fetch_object($result)) { echo "object $n"; $name[$n] = $row->name; $link[$n] = $row->link; parse_str($link[$n]); $catid[$n] = $id; echo $link[$n].""; echo $name[$n].""; echo $catid[$n].""; echo "end object"; $n++; } echo "\$link has ".sizeof($link)." array members"; $sql = "SELECT * FROM `jos_content` WHERE `catid` = "; $i = 0; while($i != sizeof($catid)) { $sql .= "\"$catid[$i]\""; if($i != sizeof($link) -1) { $sql .= " OR `catid` = "; } $i++; } $sql .= " AND CHAR_LENGTH('introtext') < 30 AND CHAR_LENGTH('fulltext') < 30"; for anyone wondering why I set catid in the first while loop it is because parse_str() overwrites the value of the variables if they already have a value in the code (in this case $id) so I choose to put it into a seperate array as soon as it is set to avoid loosing each value as a new one is parsed out of the $link array. If this is not the best way any suggestions are gladly welcome Link to comment https://forums.phpfreaks.com/topic/104856-problems-getting-a-substring-of-a-mysql-field/#findComment-538962 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.