Jump to content

Using while loop to update variable names, not working.


Space_monkey

Recommended Posts

I have two pieces of code which basically fetch data from my database then display it on the page. It was working fine until I had two rows in the database. I decided I would try to use the while loop to update the variables name which would store my data by 1 each loop. So i would end up with a set of variables for each row. Then later on I could use a similar loop to display them.

<?php
$loopno = 0;
$i = 0;

$query = mysql_query("
	SELECT *
	FROM notes
	WHERE userid=$UserID AND level='2'
	");
while ($row = mysql_fetch_assoc($query))

{
	$level2content = $row['content'];
	$level2title = $row['title'];
	$level2noteid = $row['noteid'];


	${$level2content.$i} = $level2content;
	${$level2title.$i} = $level2title;
	${$level2noteid.$i} = $level2noteid;

	$loopno = $loopno + 1;
	$i = $i + 1;
}
?>

 

The data is then displayed using this

 

<?php

if (#condition to determine if 1 loop is needed)
{
$loopno = 0;
$a = 0;
while ($loopno != $loops_required)
	{		
	echo "<H4 class='childtitle' id='level2title' contenteditable='true'>".${$level2title.$a}."noteid=".${$level2noteid.$a}." </H4>";

	echo "<div class='level2content'>";
	echo "<div id='sendlevel2content' contenteditable='true'>";
	echo ${$level2content.$a};
	echo "</div>";

	echo "</div>";

	$loopno = $loopno + 1;
	$a = $a + 1;
	}
}

else
	die
?>

I thought this would work. To me it seems like the cleanest way to do it but when i try doing it with two rows in the database I end up with two sets of the last loop being displayed but only one of them is populated with data. I presume this means the first loop isn't working.

 

I also thought I could put the "display" part of the code inside the while loop of the "fetching" code. This would eliminate a while loop and I would not need to use my funny way of changing variable names which might be the problem? How would this normally be done? A few suggestions would be great.

Instead of using variable variables, use an array:

<?php
$query = mysql_query("
	SELECT *
	FROM notes
	WHERE userid=$UserID AND level='2'
	");
$level2 = array();
while ($row = mysql_fetch_assoc($query))

{
                $level2[] = array('content'=>$row['content'],'title'=>$row['title'],'noteid'=>$row['noteid']);
}
?>

 

Display:

<?php

if (#condition to determine if 1 loop is needed)
{
        foreach ($level2 as $i => $content_array) {
	echo "<H4 class='childtitle' id='level2title' contenteditable='true'>{$content_array['title']} noteid={$content_array['noteid']}</H4>";
	echo "<div class='level2content'>";
	echo "<div id='sendlevel2content' contenteditable='true'>";
	echo $content_array['content'];
	echo "</div>";
	echo "</div>";
	}
}

else
	die
?>

 

Note: not checked for syntax errors.

 

Ken

Awesome, this is way cleaner and I think i understand how it works.

 

but could you explain this line a little

foreach ($level2 as $i => $content_array)

 

I get that it loops through all the possible values of $level2 but not sure what $i or $content_array is.

 

Thanks

Thanks for the help guys! I think I am slowly getting it. I just have a couple more questions in relation to the foreach loop.

 

My problem is that I am using jQuery ajax to send, as one example, the contents of the "sendlevel2content" div back to the database. (Not sure if there is a better way to achieve this that I should look into.)

 

I think my jquery is pretty standard and looks like this.

// Sets contents of id to variables
    var sendlevel1content = $('#sendlevel1content').html();
var level1title = $('#level1title').html();
var sendlevel2content = $('#sendlevel2content').html();
var level2title = $('#level2title').html();

//Sends variables to appropriate PHP file.
     $.ajax({

                 url: 'saveToDb.php',

                 type: 'POST',

                 data:	{
                         sendlevel1content: sendlevel1content,
					 level1title: level1title,
					 sendlevel2content: sendlevel2content,
					 level2title: level2title,
					}
             });
}

 

So my question is how would I refer to each loop individually and send the contents of the div off to the database. Hope that made sense.

 

I tried putting a counter in the loop like:

<?php
$count	= 0;
foreach ($level2 as $i => $content_array)
	{
		echo "<H4 class='childtitle' id='level2title".$count."' contenteditable='true'>{$content_array['title']} noteid={$content_array['noteid']}</H4>";
		echo "<div class='level2content'>";
		echo "<div id='sendlevel2content".$count."' contenteditable='true'>";
		echo $content_array['content'];
		echo "</div>";
		echo "</div>";

		$count = $count + 1;
	}
?>

 

but it didn't seem to work.

Also had a look at a list of PHP array functions but nothing stood out as being helpful.

 

Any ideas?

Anyone have a idea. I cant seem to find a solution, my google fu is failing me.

 

Although this might be more a of a javascript or jQuery question now, does anyone know how I would reference the divs in the foreach loop?

 

If its not possible I will have to try another approach, but i think any loop will have the same problem.

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.