Jump to content

Array issue??


law

Recommended Posts

I wanted to create a 'news' update feature that an admin could login to the site and add 'news'.

 

Form for inputing news (very basic)

<div id="stuff">
<?php
include_once("./dbconfig.php");
$n="";
echo 
"<form name='news' action='./processnews.php' method='post' enctype='multipart/form-data'>
<table cellspacing='16' cellpadding='0' border='0'  >
<tr>
	<td class='form_field' valign='top' align='right'>Avalibility of this News </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>

	<td class='form_text'>
<input type='radio' name='Avalib'  value='1'   > Availible to Administration ONLY<br>
<input type='radio' name='Avalib'  value='2'   > Availible to Members and Administration<br>
<input type='radio' name='Avalib'  value='3'   > Availible to Everyone<br>

	</td>
</tr>
<tr>
	<td class='form_field' valign='top' align='right'>Date </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
	<td class='form_text'>
<textarea name='Date' rows=1 cols=25 ></textarea>

	</td>
</tr>

<tr>
	<td class='form_field' valign='top' align='right'>News </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
	<td class='form_text'>
<textarea name='News' rows=4 cols=25 ></textarea>

	</td>
</tr>


<tr><td colspan=3 align='center'><input type='submit' value='Submit'>    <input type='reset' value='Reset'></td></tr>
</table>
</form>
";
?>
</div>

 

This code posts data from form above.

<?php 
session_start();
if(isset($_SESSION['admin_user'])){
$tmp=$_SESSION['admin_user'];	

$var0 = $_POST['Avalib'];
$var1 = stripslashes($_POST['News']);
//	$var2 = stripslashes($_POST['userid']);
$var3 = $_POST['Date'];
include("./dbconfig.php");
$sql = mysql_query("SELECT id FROM admin WHERE nickname = '$tmp'") or die(mysql_error());
//echo "$tmp,$var0,$var1";
$num = mysql_fetch_array($sql);
	include_once('./includes/adminhead.inc');
	include_once('./includes/adminlogo.inc');
	include_once('./includes/adminmenu.inc'); 	
	$result = mysql_query("INSERT INTO news(adminid, news, viewership, date) VALUES('$num[0]', '$var1', '$var0', '$var3')");
	print "** Thank you <B><font color=#adfcff>";
	print $tmp;
	print "</B></font> your news has been registered. **";
include_once('./includes/membernews.inc');
include_once('./includes/adminfooter.inc');      
} else {
header('Location:admin.php?notloggedin=1');
}
?>

 

This is SUPPOSED TO retrieve my news and uses sessions to decide if the user is an admin/regular user/or not logged in

the viewership field determins what user can see what data.. ALL OF THAT IS IRRELEVANT.. the code works.. kinda

<?php
include_once("./dbconfig.php");
if (isset($_SESSION['admin_user'])){
$tmp=$_SESSION['admin_user'];
$adminq="SELECT news FROM news WHERE viewership > 0";
$adminsql=mysql_query($adminq);	
//$adminrow=mysql_fetch_assoc($adminsql);
//echo "$adminsql";
$i=0;
while($row = mysql_fetch_assoc($adminsql)){
			foreach($row as $key => $value){
				$data[$i][$key] = $value;
			}
			$i++;
		}
		print_r($data);
//==================================================================	
} elseif (isset($_SESSION['ses_user'])){
			$tmp=$_SESSION['ses_user'];
//==================================================================
		} else {

		}
?>
</div>

 

HERE IS THE OUTPUT I GET

=================================================

Array ( [0] => Array ( [news] => hgjkhgkjhgkjghjkhgkjhgk newline extendedpastthebox----------- ) [1] => Array ( [news] => News and Events from today ) [2] => Array ( [news] => Ipsum Lorium ) [3] => Array ( [news] => big news and its important ) [4] => Array ( [news] => big news and its important ) )

=================================================

 

How can i take the above information and remove the tags and crap from it so that it can be formatted and readable within a table???

Link to comment
https://forums.phpfreaks.com/topic/88317-array-issue/
Share on other sites

you have looped twice mate you can either use foreach function or while on that very same variable;

 

but anyways try and change the "print($data)" to print($data[0]);

 

if it shows any of your posts because you are ptinting the contents of the whole array but not a specific part of the array :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/88317-array-issue/#findComment-451963
Share on other sites

Ok i did what you said, and i also cut out the other parts of the code im not using currently.

Here is what i have now

<?php
include_once("./dbconfig.php");
if (isset($_SESSION['admin_user'])){
$tmp=$_SESSION['admin_user'];
$adminq="SELECT news FROM news WHERE viewership > 0";
$adminsql=mysql_query($adminq);	
//$adminrow=mysql_fetch_assoc($adminsql);
//echo "$adminsql";
$i=0;
while($row = mysql_fetch_row($adminsql)){
			foreach($row as $key => $value){
				$data[$i][$key] = $value;
			}
			$i++;
		}
		print_r($data[0]);
//==================================================================	
//	} elseif (isset($_SESSION['ses_user'])){
//				$tmp=$_SESSION['ses_user'];
//==================================================================
		} else {

		}

here is what it prints

"Array ( [0] => hgjkhgkjhgkjghjkhgkjhgk newline extendedpastthebox----------- )"

Still contains the ARRAY ([0] => "text" and now it only displays one of the arrays, So this is NOT what i wanted to do. I would like for them to all display within an easy to read table.

Link to comment
https://forums.phpfreaks.com/topic/88317-array-issue/#findComment-452412
Share on other sites

if all you are selecting from the table is news why put anything into an array when it is already an array

 

<?php
include_once("./dbconfig.php");
if (isset($_SESSION['admin_user'])){
$tmp=$_SESSION['admin_user'];
$adminq="SELECT news FROM news WHERE viewership > 0";
$adminsql=mysql_query($adminq);	
//$adminrow=mysql_fetch_assoc($adminsql);
//echo "$adminsql";
while($row = mysql_fetch_assoc($adminsql)){
echo $row['news'];
}
?>

 

Ray

 

Link to comment
https://forums.phpfreaks.com/topic/88317-array-issue/#findComment-452419
Share on other sites

that just dawned on me aswell, i was just using a suggestion that someone gave me a while ago... so here is they new code.. im completely new to loops and things so i have no idea why this doesn't work

 

include_once("./dbconfig.php");
if (isset($_SESSION['admin_user'])){
$tmp=$_SESSION['admin_user'];
$adminq="SELECT news FROM news WHERE viewership > 0";
$adminsql=mysql_query($adminq);	
//$adminrow=mysql_fetch_assoc($adminsql);
//echo "$adminsql";
$ii=0;
$num =mysql_num_rows($adminsql);
$row = mysql_fetch_row($adminsql);
echo "<table width='100' align='center'>";
for($i=0; $i<=$num; $i=$i+1)
{
	if($i % 2 == 0)
	{
		echo "<tr>";
		echo "<td style='background-color:blue'>";
		echo $row[$i];
		echo "</td>";
		echo "</tr>";
	}
	else
	{
		echo "<tr>";
		echo "<td style='background-color:none'>";
		echo $row[$i];
		echo "</td>";
		echo "</tr>";
	}
}
echo "</table>";
{
else}
{
?>

 

im trying to get the $row to increment itself to display all of the news instead of the news that is @ array 0.. how can i do this.. (currently it only displays "hgjkhgkjhgkjghjkhgkjhgk newline extendedpastthebox-----------" which is my test text)

 

Link to comment
https://forums.phpfreaks.com/topic/88317-array-issue/#findComment-452474
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.