Jump to content

[SOLVED] Column Creation Help!


The Little Guy

Recommended Posts

I am having trouble with this loop:

		while($row = mysql_fetch_array($sql)){
		if($curCol != $row['column']){
			if($row['column'] == 1)
				$width = '30%';
			else
				$width = '68.5%';
			$curCol = $row['column'];
			echo '<div style="float:left;width:'.$width.';">'."\n";
		}
			if(file_exists('../incl/widgets/'.strtolower(str_replace(' ', '_', $row['name'])).'.php')){
				include '../incl/widgets/'.strtolower(str_replace(' ', '_', $row['name'])).'.php';
				echo '<div style="clear:both;"></div>'."\n";
			}
		if($curCol != $row['column'] || $i == $totalR){
			echo '</div>'."\n";
			echo '<div style="width:10px;float:left;"> </div>';
		}
		$i++;
	}

 

This line is making the page display all funky: $curCol = $row['column'];

I have two div columns, and the loop places widgets into those two columns according to the database. Widget one gets placed on the page perfectly. Widget two is the widget I am having troubles with. If I keep the above line where it is in the loop, and place both widgets into column one the page displays fine. If I decide to take widget two and place it into column two, the page gets all messed up. It stays in column 1 because it then doesn't go into the last if statement. But if I move the above line into the last if statement, then the second column displays fine, but if I move the widget back to column one, then the page doesn't display correctly again....

 

Any help? (Please let me know if you need a better explanation, thanks!)

 

Here is the pages full code:

<?php
if($_SESSION['id'] == $_id){
	echo '<div style="text-align:right;padding-right:10px;height:14px;"><a class="editHomePage" href="">Add/Remove Widgets</a></div><br />';
}
$sql = mysql_query("SELECT * FROM userWidgets u LEFT JOIN widgets w ON (u.widget = w.id) WHERE owner = '$_id' ORDER BY `column`, `row`")or die(mysql_error());
if(mysql_num_rows($sql) > 0){
	$curCol = 0;
	$curCol2 = 0;
	$totalR = mysql_num_rows($sql);
	$i = 1;
	while($row = mysql_fetch_array($sql)){
		if($curCol != $row['column']){
			if($row['column'] == 1)
				$width = '30%';
			else
				$width = '68.5%';
			$curCol = $row['column'];
			echo '<div style="float:left;width:'.$width.';">'."\n";
		}
			if(file_exists('../incl/widgets/'.strtolower(str_replace(' ', '_', $row['name'])).'.php')){
				include '../incl/widgets/'.strtolower(str_replace(' ', '_', $row['name'])).'.php';
				echo '<div style="clear:both;"></div>'."\n";
			}
		if($curCol != $row['column'] || $i == $totalR){
			echo '</div>'."\n";
			echo '<div style="width:10px;float:left;"> </div>';
		}
		$i++;
	}
	echo '<div style="clear:both;"></div>'."\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/167159-solved-column-creation-help/
Share on other sites

(What you have looks a bit complicated)

 

Can you approach the problem differently?  Why not query your database and place the "widgets"

into an array of the available columns to get it all organized first, and then place the widgets?  Perhaps a picture would help illustrate how you want it to be displayed?

OK...

 

I don't know how well a picture would be, because the page is supposed to be modified by the user. He chooses what he wants in each column. Right now I have two widgets (more are coming) but the user can place both widgets on the left (in any order), both on the right (in any order), or one in each column. As of now, I have two columns, I may add a third column, but would be getting a little cramped.

 

Could you explain your array suggestion a little more?

What I'm saying is, in your loop right there, you're extracting the result set info, echoing html, and trying to plan columns all in the while loop..  That's a lot to do and manage for one loop.  Break it down.  You want each widget to go into a certain column, and in a certain order (likely the order they come out of the SQL statement).

 

This is what I'm thinking of.  Tell me if it's off track:

<?php
$columns = array();
while($row = mysql_fetch_array($sql)) {
    
    //check that widget exists
    if(file_exists('../incl/widgets/'.strtolower(str_replace(' ', '_', $row['name'])).'.php')) {

        //get column destination, place into array
        $col = $row['column'];//I am assuming this is an integer
        $columns['column' . $col][] = '../incl/widgets/' . strtolower(str_replace(' ', '_', $row['name'])) . '.php';

    }
}

    //build webpage layout, column 1
    $col_1_width = '30%';
    foreach($columns['column1'] as $widgy)
    {
        echo '<div style="float:left;width:'.$col_1_width.';">'."\n";
            include($widgy);
            echo '<div style="clear:both;"></div>'."\n";
    }
    
    //"close" that column ?
    echo '</div>'."\n";
    echo '<div style="width:10px;float:left;"> </div>';
    
    
    
    //main content?
    //build webpage layout column 2
    $col_2_width = '68.5%';
    foreach($columns['column2'] as $widgy)
    {
    }?>

 

Not tested..

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.