PDA

View Full Version : Arraying Field PHP



RushScripting
January 28th, 2005, 11:43 AM
Ok in Mysql I have a field group_links with content such as add_news, add_project, edit_news, edit_project on and on. Ok well I was going to array this info but im guessing I have lost my mind because I have forgotted how to break the field group_links into seperate values so i can array them. For Example:




$query = mysql_query(BLAH);
$row = mysql_fetch_array();
foreach( $row['group_links'] as $k => $v ){
echo"blah";
}


This doesn't work because it doesn't break appart the string $row['group_links'] . How do i go about breaking the string into different parts.

binime
January 28th, 2005, 12:48 PM
im not 100% sure what you mean



foreach( $row as $k => $v ){
echo "<BR>{$K}<BR>{$P}<HR>";
}


the way you was doing was making $row['group_links'] an array $row["group_links"][] you dont want that :P

RushScripting
January 28th, 2005, 05:14 PM
O my bad. I missed a VERY important part of the code.



$query = mysql_query("SELECT * FROM ahhs_groups WHERE group_id=".$_SESSION['group_id']."");

$row = mysql_fetch_array($query);

$link = array($row['group_links']);

$links .="<h2>Admin Links</h2>
<p>";

foreach( $link as $k => $v ){

$links .="&middot;<a href='".$_SERVER['PHP_SELF']."?".$session_link."act=".$v."'>Add News</a><br/>";
}

That is the code as it is. I have tried to array it already and it failed because $row['group_links'] is a string and it wont array the values (the values are: add_news, edit_news, etc.). I am needing to break apart this string into an array.

Cybernoid
January 28th, 2005, 06:38 PM
$link = array($row['group_links']);


That is the code as it is. I have tried to array it already and it failed because $row['group_links'] is a string and it wont array the values (the values are: add_news, edit_news, etc.). I am needing to break apart this string into an array.

What exactly are you trying to do? You're making an array called $link, and you insert a value from $row array into it. BUT you're not telling the $row-array which value, but instead you're typing something incomprihensible...

To create an array:


$someArray = array($value1, $value2, $value3);



To see what's inside an array:


for ($j = 0; $j < sizeof($someArray); $j++)
{
echo $someArray[$j];
echo "<br />";
}


So, what you do is: you create an array and put _one_ value in it. That is, if your $row-array has a value by the name of "group_links", which I doubt.

You're probably looking for something like this:


$query="whatever";
$result = mysql_query($query);

for($i=1;$i<(mysql_num_rows($result)+1);$i++)
{
$row_array = mysql_fetch_row($result);
$id=$row_array[0];
$group_link1=$row_array[1];
$group_link2=$row_array[2];

echo("$group_link1 $group_link2 <\br> ");
}


Or something. I'm not quite following you.

Cybernoid

RushScripting
January 30th, 2005, 02:33 PM
Ok as i said before the value of $row['group_links'] is something like add_news, edit_news . What i tried to do is array the value thinking the comma already in the string would allow the array() function to array them. But i was wrong in that it used the $row['group_links'] as a string add_news, edit_news . I am needing a way to seperate the string into the two values add_news and edit_news. I can't just set the value because the info is dynamic and different for each group. So in turn i am needing to array a string add_news, edit_news.