Jump to content

Recommended Posts

I need $row['con_id'] outside the function so I can put it into a query, but simply putting global in front of it won't work. What would be the right way to do?

 

This portion of code is INSIDE a function:

 

//Loop through the array of data
while ($row = mysqli_fetch_array ($data)) 
{
	echo "<table padding='0' margin='0' class='knuffixTable'>";
	echo "<input type='hidden' name='con_id' value='<?php echo " . $row['con_id'] . "; ?>' />";
	echo "<tr><td width='65px' height='64px' class='avatar_bg' rowspan='2' colpan='2'><img src='$avatar_path' alt='avatar' /></td><td class='knuffix_username'><strong>" . $user_name;
	echo "</strong><br />" . $row['category'] .  " | " . date('M d, Y', strtotime($row['contributed_date'])) . "</td></tr><tr><td>";
	echo "<form action='' method='post'>
			<input type='submit' name='plusVote' value='Y' />
			<input type='submit' name='minusVote' value='N' />
			</form></td><td class='votes'>Y[ - ] | N[ - ]</td></tr>";
	echo "<tr><td class='knuffix_name' colspan='3'><strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td colspan='2' class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "</table>";

 

 

The PHP documentation does not show any examples how to GLOBALIZE a mysqli function. I'd need a way to use $row['con_id'] outside the function for the voting script I'm writing.

 

Thanks for the suggestions.

thanks for the suggestion cunoodle2, your version works but the problem I'm having is that it only prints out the LAST printed table in the while loop, it does not print out the con_id of the corresponding table where the submit was pressed.

 

So more my scripts become complex so more I realize I have a HUGE linking issue. In this sense thorpe's request for the full function is justified (I just posted a portion of it above) and here is the full function:

 

<?php 
function knuffix_list ($query) {

define ('AVATAR_UPLOADPATH', 'avatar/');
$target = AVATAR_UPLOADPATH;

$user_name = $_SESSION['user_name'];
$user_id = $_SESSION['user_id'];
$user_avatar = $_SESSION['user_avatar'];


    $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$query_user = "SELECT * FROM user";
$query_user_run = mysqli_query ($dbc, $query_user) or die (mysqli_error ($dbc)); 
$assoc = mysqli_fetch_assoc ($query_user_run); 

$data = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc));

$user_name_assoc = $assoc['nickname'];

$avatar_path = $target . $assoc['avatar'];

global $global_con_id;

//Loop through the array of data
while ($row = mysqli_fetch_array ($data)) 
{

	echo "<table padding='0' margin='0' class='knuffixTable'>";
	/* echo "<input type='hidden' name='con_id' value='<?php " . $row['con_id'] . "; ?>' />"; */
	echo "<tr><td width='65px' height='64px' class='avatar_bg' rowspan='2' colpan='2'><img src='$avatar_path' alt='avatar' /></td><td class='knuffix_username'><strong>" . $user_name_assoc . "___" . $global_con_id = $row['con_id'];
	echo "</strong><br />" . $row['category'] .  " | " . date('M d, Y', strtotime($row['contributed_date'])) . "</td></tr><tr><td>";
	echo "<form action='' method='post'>
			<input type='submit' name='plusVote' value='Y' />
			<input type='submit' name='minusVote' value='N' />
			</form></td><td class='votes'>Y[ - ] | N[ - ]</td></tr>";
	echo "<tr><td class='knuffix_name' colspan='3'><strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td colspan='2' class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "</table>";
}
mysqli_close($dbc);

}

?>

 

 

The PROBLEMS I'm having:

 

FIRST I used to showcase the avatar and username with the session variables, BUT this of course only works when the user is logged in and the sessions are set, if the user is not logged in then the avatar will not show up and the username will not show up either, so I thought I may try to fetch the username and avatar from the database but this does not work either because it will simply take a random user and NOT THE user corresponding to that table, which is the user who contributed the contribution.

 

This is a linking issue, I need to find out how I can fetch the contribution data from the table CON and then show up the username PLUS avatar of the user, doing it over SESSIONS did not work, as described earlier, fetching it of the database table called USER will not work either, because I don't know how to link the right contribution from database table CON to the right user in the database table USER, I have a foreign key called user_id in CON but no foreign key in USER.

 

SECOND

I tested the con_id variable with this small test script:

 

// SORTING BY POPULARITY

$plus_vote = $_POST['plusVote'];
$minus_vote = $_POST['minusVote'];

if ($plus_vote || $minus_vote) {


echo "test " . $global_con_id;

}

 

When I now press the input buttons $plus_vote OR $minus_vote, I always get the con_id "4" which is the id of the last table in the while loop, here again a huge linking issue, I don't know how to tell the input button to give me the con_id of the corresponding table.

 

I'm losing the overview at this moment.

 

I'd appreciate any suggestion, to help me see this a bit clearer.

Here's a less confusing version of the script, as of now I'm using sessions to showcase the avatar and nickname:

 

<?php 
function knuffix_list ($query) {

define ('AVATAR_UPLOADPATH', 'avatar/');
$target = AVATAR_UPLOADPATH;

$user_name = $_SESSION['user_name'];
$user_id = $_SESSION['user_id'];
$user_avatar = $_SESSION['user_avatar'];


    $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);				
$data = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc));
$avatar_path = $target . $user_avatar;



global $global_con_id;

//Loop through the array of data
while ($row = mysqli_fetch_array ($data)) 
{

	echo "<table padding='0' margin='0' class='knuffixTable'>";
	/* echo "<input type='hidden' name='con_id' value='<?php " . $row['con_id'] . "; ?>' />"; */
	echo "<tr><td width='65px' height='64px' class='avatar_bg' rowspan='2' colpan='2'><img src='$avatar_path' alt='avatar' /></td><td class='knuffix_username'><strong>" . $user_name . "___" . $global_con_id = $row['con_id'];
	echo "</strong><br />" . $row['category'] .  " | " . date('M d, Y', strtotime($row['contributed_date'])) . "</td></tr><tr><td>";
	echo "<form action='' method='post'>
			<input type='submit' name='plusVote' value='Y' />
			<input type='submit' name='minusVote' value='N' />
			</form></td><td class='votes'>Y[ - ] | N[ - ]</td></tr>";
	echo "<tr><td class='knuffix_name' colspan='3'><strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td colspan='2' class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "</table>";
}
mysqli_close($dbc);

}

?>

 

To not throw different questions together, in this thread I'm asking how to echo the RIGHT con_id with the submit buttons. Meaning the con_id OF THAT table I clicked the submit should be displayed, right now it's just displaying the con_id of the last table in the printed while loop.

Firstly, the idea of globals and functions do not mix. Functions are there to isolate a piece of code into a reusable and abstract chunk, forcing them to use global variables breaks this idea entirely.

 

Secondly, the main purpose of this functions seems to be to loop through a result set and display a table for each result. Why are you trying to force it to do something else as well?

 

Functions should preform one specific task, and one specific task only.

Firstly, the idea of globals and functions do not mix. Functions are there to isolate a piece of code into a reusable and abstract chunk, forcing them to use global variables breaks this idea entirely.

 

Secondly, the main purpose of this functions seems to be to loop through a result set and display a table for each result. Why are you trying to force it to do something else as well?

 

Functions should preform one specific task, and one specific task only.

Ok that makes sense, here's the reason why I have a function in the first place:

 

// SORTING FUNCTIONALITY - START
// when ALL is set or NONE of the possibilities are set
if (($select_category == 'All') || (!isset($select_category)) && (!isset($sort_date_var))) {

$query	= "SELECT * FROM con";

knuffix_list ($query);

// when the category is set BUT NOT date	
} elseif (isset($select_category) && !isset($sort_date_var)) {

$query = "SELECT * FROM con WHERE category = '$select_category'";

knuffix_list ($query);

// when the date is set BUT NOT the category
} elseif (!isset($select_category) && isset($sort_date_var)) {

$query = "SELECT * FROM con ORDER BY contributed_date $sort_date_var";

knuffix_list ($query);

// when the category is set AND the date is set
} elseif (isset($select_category) && isset($sort_date_var)) {

$query = "SELECT * FROM con WHERE category = '$select_category' ORDER BY contributed_date $sort_date_var";

knuffix_list ($query);
}
// END

 

I'm using the function to avoid duplicate code, the code above is a sorting functionality, it sorts by category AND/OR orders by date.

 

Now I want to implement a rating thumbs up/down functionality, and this was the way I came up with. I've finished writing the rating functionality which per se works as is, the only problem is as stated, as soon as the submit Y  or N buttons are being clicked then the rating is always being added to the last printed table in the while loop, instead it should do it to the corresponding table.

 

Now that you know the full story, what would you rather recommend, how should I solve this problem instead?

 

This solution was the most obvious one to me, though, I didn't know that it's bad practice to mix in globals.

Globals are bad in general.  Never use them, as they ultimately make code unmanageable.  Pass everything your function needs in order to work through its argument list.  If you need to use a value that was created/calculated from within the function outside of it, return it.

Thanks for pointing that out I will grab a book out and learn the proper ways of functions.

 

In the meanwhile I think I somewhat know how to solve the rating issue, I think I'll have to create a function for then rating buttons and the insert that function in the while loop, like this:

 

rate_func(ID); 

 

The id should be automatically incremented that way every table has it's own id, that way I'd be able to correspond the correct rating to the correct table.

 

It was just an idea, but I'll have to learn the proper ways of functions first until I approach this, I thought it would take a while until I dive into functions, it happened quicker than I expected.

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.