Jump to content

collecting info from the database then loop into function


saltedm8

Recommended Posts

I know I keep asking questions, but I am still learning and have come across another issue

 

I am trying to call info from a database, and display it as a menu,

 

here is what I have

 

<?php

include("connections.php");

$chop = mysql_query("SELECT * FROM table1(field2,field3,field4,field5,)VALUES('$field2','$field3','$field4','$field5')  ");

$menu = array('item'=>$chop, 'div'=>'this', 'count'=>5);

function call($menu) {

  	$var1 = '<div id="';
   	$var1 .= $menu['div'] . '">' ."\n";
   	$var1 .= '<ul>'."\n";

   for ($i=0;$i<$menu[count];$i++){

               $var1 .= '<li>' . $menu[item] . '</li>'."\n";

   }

	$var1 .= '</ul>' ."\n";
	$var1 .= '</div>';
                        
return $var1;			

}	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php echo call($menu);	?>

</body>
</html>

 

unfortunately it does not seem to be working as I hoped, its displaying the html ok, and the loop is working... but its not getting the info from the database.. I even tried an array as a test first to see if this would work, and I could not even get the array working, the best I could get was the last word in the array repeating down the menu...

 

would you mind showing me where I am going wrong for both methods... thank you

Link to comment
Share on other sites

Well, you're trying to echo out the mysql_query, which you should note returns a resource. Which field, exactly, are you trying to echo out into a menu? At the top of the code below, there's a variable called $fieldName. Enter the field name you're trying to use as a string, and it should work from there.

 

I've only checked the problem area, haven't proofread the rest of your code, but give this a shot:

 

<?php
include("connections.php");
$fieldName = 'field12345';

$chop = mysql_query("SELECT * FROM `table1`");
$fetch = mysql_fetch_assoc($chop);

$menu = array (
	'div'   => 'this',
		'count' => 5
	);

function call($menu) {
$var1 = '<div id="';
$var1 .= $menu['div'] . '">' ."\n";
$var1 .= '<ul>'."\n";

for ($i=0; $i < $menu[count]; $i++) {
	$item = mysel)fetch_assoc($chop);
	$var1 .= '<li>' . $item . '</li>'."\n";
}

	$var1 .= '</ul>' ."\n";
	$var1 .= '</div>';
                        
return $var1;								
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php echo call($menu);	?>

</body>
</html>

Link to comment
Share on other sites

thanks for the shot, but I am still none the wiser

 

my end goal is for someone to submit a list of menu items to the database ( also with another text box to say what the menu div name is called ), for example I want the end result to be whatever they put in like this

 

<div id="theirdivname">
<ul>
<li><a href="/page.html"> menu item </a></li> 
<li><a href="/page.html"> menu item </a></li> 
<li><a href="/page.html"> menu item </a></li> 
<ul/>
</div>

 

so, they are defining the div's name and they are also defining the menu names and the amount of menu items, possibly using the database as an array sort of thing so it grabs all the menu items... maybe im not explaining myself very well... I hope you get what I am doing now

 

then for this script to collect that info and use a single function command call($menu) in and it displays the menu

Link to comment
Share on other sites

thanks for the shot, but I am still none the wiser

 

my end goal is for someone to submit a list of menu items to the database ( also with another text box to say what the menu div name is called ), for example I want the end result to be whatever they put in like this

 

<div id="theirdivname">
<ul>
<li><a href="/page.html"> menu item </a></li> 
<li><a href="/page.html"> menu item </a></li> 
<li><a href="/page.html"> menu item </a></li> 
<ul/>
</div>

 

so, they are defining the div's name and they are also defining the menu names and the amount of menu items, possibly using the database as an array sort of thing so it grabs all the menu items... maybe im not explaining myself very well... I hope you get what I am doing now

 

then for this script to collect that info and use a single function command call($menu) in and it displays the menu

 

instead of using just $row = mysql_fetch...

 

try a loop:

while($row = mysql_fetch...){
//obtain row data information here

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.