K
K
kulabuhov2018-04-22 16:21:36
PHP
kulabuhov, 2018-04-22 16:21:36

The problem with the output of the rating, what's the problem?

I display the rating on the page. There are 3 articles, all of them are displayed on the main page and each should have its own rating. I added it by 1, everything works, everything is written to the database, but I add it to the second one and already an error.

I display the rating itself with this code:

<?
require('form.php');

$userrating="2"; // имя пользователя за кооторого голосуем, или ID

echo rating_bar($userrating,5);
?>


Article 1 has userrating="1" , article 2 has a value of 2 respectively

Next, the form.php file itself
<?php
function rating_bar($id, $units = '', $static = '')
{
    require('confrate.php');
    $rating_tableName = 'ratings';
    $rating_path_db = '';
    $rating_path_rpc = '';
    $rating_unitwidth = 17;
    $rating_conn = mysql_connect($database_host, $database_username, $database_password) or die('Error connecting to mysql');
    $ip = $_SERVER['REMOTE_ADDR'];
    if (!$units)
    {
        $units = 10;
    }
    if (!$static)
    {
        $static = false;
    }
    $query = mysql_query("SELECT total_votes, total_value, used_ips FROM ".$database_name.".".$rating_tableName." WHERE id='".$id."' ") or die(" Error: " . mysql_error());
    if (mysql_num_rows($query) == 0)
    {
        $sql = "INSERT INTO ".$database_name.".".$rating_tableName." (`id`,`total_votes`, `total_value`, `used_ips`) VALUES ('".$id."', '0', '0', '')";
        $result = mysql_query($sql);
    }
    $numbers = mysql_fetch_assoc($query);
    if ($numbers['total_votes'] < 1)
    {
        $count = 0;
    }
    else
    {
        $count = $numbers['total_votes'];
    }
    $current_rating = $numbers['total_value'];
    $tense = ($count == 1) ? "vote" : "votes";
    $voted = mysql_num_rows(mysql_query("SELECT used_ips FROM ".$database_name.".".$rating_tableName." WHERE used_ips LIKE '%" . $ip . "%' AND id='" . $id . "' "));
    $rating_width = @number_format($current_rating / $count, 2) * $rating_unitwidth;
    $rating1 = @number_format($current_rating / $count, 1);
    $rating2 = @number_format($current_rating / $count, 2);
    if ($static == 'static')
    {
        $static_rater = array();
        $static_rater[] .= "\n" . '<div class="ratingblock">';
        $static_rater[] .= '<div id="unit_long' . $id . '">';
        $static_rater[] .= '<ul id="unit_ul' . $id . '" class="unit-rating" style="width:' . $rating_unitwidth * $units . 'px;">';
        $static_rater[] .= '<li class="current-rating" style="width:' . $rating_width . 'px;">Текущий ' . $rating2 . '/' . $units . '</li>';
        $static_rater[] .= '</ul>';
        $static_rater[] .= '<p class="static">' . $id . '. Рейтинг: <strong> ' . $rating1 . '</strong>/' . $units . ' (' . $count . ' ' . $tense . ' cast) <em>This is \'static\'.</em></p>';
        $static_rater[] .= '</div>';
        $static_rater[] .= '</div>' . "\n\n";
        return join("\n", $static_rater);
    }
    else
    {
        $rater = '';
        $rater .= '<div class="ratingblock">';
        $rater .= '<div id="unit_long' . $id . '">';
        $rater .= '  <ul id="unit_ul' . $id . '" class="unit-rating" style="width:' . $rating_unitwidth * $units . 'px;">';
        $rater .= '     <li class="current-rating" style="width:' . $rating_width . 'px;">Текущий ' . $rating2 . '/' . $units . '</li>';
        for ($ncount = 1; $ncount <= $units; $ncount++)
        {
            if (!$voted)
            {
                $rater .= '<li><a href="/rat/db.php?j=' . $ncount . '&amp;q=' . $id . '&amp;t=' . $ip . '&amp;c=' . $units . '" title="' . $ncount . ' из ' . $units . '" class="r' . $ncount . '-unit rater" rel="nofollow">' . $ncount . '</a></li>';
            }
        }
        $ncount = 0;
        $rater .= '  </ul>';
        $rater .= '  <p';
        if ($voted)
        {
            $rater .= ' class="voted"';
        }
        $rater .= '  </p>';
        $rater .= '</div>';
        $rater .= '</div>';
        return $rater;
    }
}
?>


Well, in the header styles and script
<script type="text/javascript" src="rating.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />


The following error is thrown:
Fatal error: Cannot redeclare rating_bar() (previously declared in Z:\home\localhost\www\form.php:2) in Z:\home\localhost\www\form.php on line 79


those. the problem is in this line:
function rating_bar($id, $units = '', $static = '')
, right? In what I just do not understand.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Decadal, 2018-04-22
@kulabuhov

require('form.php');
change to
require_once('form.php');

Y
Yan-s, 2018-04-22
@Yan-s

Из ошибки следует, что вы пытаетесь переопределить уже существующую функцию rating_bar
Скорее всего вы дважды подключаете к странице файл form.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question